Is it possible to simplify it somehow:

function is_iOS() { var iDevices = ['iPad Simulator', 'iPhone Simulator', 'iPod Simulator', 'iPad', 'iPhone', 'iPod']; while (iDevices.length) { if (navigator.platform === iDevices.pop()) return true; } return false; } function fixedRec(el) { if($(document).scrollTop() >= el.prev().offset().top + el.prev().outerHeight()) { if($(document).scrollTop() < el.parent().outerHeight() - ($(window).outerHeight() / 1.5)) { if(is_iOS() || $(window).width() < 1000){ el.removeClass('fixed').addClass('absolute').css({top: (top - 150)}); } else { el.removeAttr('style').removeClass('absolute').addClass('fixed'); } } else { el.removeAttr('style').removeClass('fixed').addClass('absolute'); } } else { el.removeAttr('style').removeClass('absolute').removeClass('fixed'); } } 

    2 answers 2

    Instead of this tin

     function is_iOS() { var iDevices = ['iPad Simulator', 'iPhone Simulator', 'iPod Simulator', 'iPad', 'iPhone', 'iPod']; while (iDevices.length) { if (navigator.platform === iDevices.pop()) return true; } return false; } 

    Can do.

     function checkIOS() { var iDevices = ['iPad Simulator', 'iPhone Simulator', 'iPod Simulator', 'iPad', 'iPhone', 'iPod']; return iDevices.includes(navigator.platform); } console.log(checkIOS()); 

    There is another way with .test (), but you already have an array.
    Are you sure that there should be an "OR" in checking for devices and window size?

    • Array.prototype.includes missing in IE/Edge . I would use some . - user236014

    is_iOS rewritten using Array.prototype.some

     function is_iOS() { var iDevices = ['iPad Simulator', 'iPhone Simulator', 'iPod Simulator', 'iPad', 'iPhone', 'iPod']; return iDevices.some(dev => dec === navigator.platform); } 

    do fixedRec little less convex, due to replacing else with return .
    Be sure to add comments that happens in each case. For example: Долистали до следующего элемента .

     function fixedRec(el) { let top = $(document).scrollTop(); if (top < el.prev().offset().top + el.prev().outerHeight()) { // comment el.removeAttr('style').removeClass('absolute').removeClass('fixed'); return; } if (top >= el.parent().outerHeight() - ($(window).outerHeight() / 1.5)) { // comment el.removeAttr('style').removeClass('fixed').addClass('absolute'); return; } if(is_iOS() || $(window).width() < 1000){ el.removeClass('fixed').addClass('absolute').css({top: (top - 150)}); } else { el.removeAttr('style').removeClass('absolute').addClass('fixed'); } }