Yobd Digital Yobd Digital - 6 months ago 13
Javascript Question

jQuery functions not working in IE or on mobile devices

Several of my jQuery functions aren't working in IE or on my iOS device. I don't have an Android so I don't know about that.

Function One:



function keepGoing(anchor, id, scrollSpeed = 1000 ) {
$(anchor).css("cursor","pointer");
$(anchor).click(function() {
$('html, body').animate({
scrollTop: $(id).offset().top
}, scrollSpeed );
});
}


Function Two:



function showOnScroll(targetClass,cssAttr,attrValue) {
$(window).scroll(function(event) {

$(targetClass).each(function(i, el) {
var el = $(el);
if (el.visible(true)) {
el.css(cssAttr,attrValue);
}
});

});
}
showOnScroll('.fast-track-img','margin-left','unset');
showOnScroll('.own-speed-img','margin-left','unset');
showOnScroll('.rising-star-img','margin-left','unset');

Answer

Problem One: function parameters default set

Function parameter defaults is very new syntax only added in ECMAScript 2015. Use something like this instead:

function(param) {
  if (param === undefined) param = val;
    // ...
} 

Problem Two:

The second problem was IE and the mobile browsers don't like margin:unset; but prefer margin:0;

Comments