I am trying to migrate to the latest jQuery version (3.4.1) for my smooth scroll effect, and got seems that something adds a display-none style to the body tag leaving my page useless.
The script is:
(function($){
var jump=function(e)
{
if (e){
e.preventDefault();
var target = $(this).attr("href");
}else{
var target = location.hash;
}
$('html,body').animate(
{
scrollTop: $(target).offset().top
},1000,function()
{
location.hash = target;
});
}
$('html, body').hide()
$(document).ready(function()
{
$('a[href^=#]').bind("click", jump);
if (location.hash){
setTimeout(function(){
$('html, body').scrollTop(0).show()
jump()
}, 0);
}else{
$('html, body').show()
}
});
})(jQuery)
It works with 1.7.1 and other more recent versions, but after 1.11.2 I get a blank page.
I am not a jQuery expert and would like to know if there might be any solution or I should try other script.
Calling .hide() on the body and html elements will add display:none to their styles. So that gives us a clue. The body is being hidden⌠and yet your code says it should be shown later. Hm! This makes me think thereâs an error thrown which is stopping code from executing.
So, I tried your script on a simple web page which has some links and divs. If I use a recent version of jQuery, then - aha! - I see this error in the JavaScript console:
Uncaught Error: Syntax error, unrecognized expression: a[href^=#]
at Function.se.error (jquery-3.4.1.min.js:2)
at se.tokenize (jquery-3.4.1.min.js:2)
at se.select (jquery-3.4.1.min.js:2)
at Function.se [as find] (jquery-3.4.1.min.js:2)
at k.fn.init.find (jquery-3.4.1.min.js:2)
at new k.fn.init (jquery-3.4.1.min.js:2)
at k (jquery-3.4.1.min.js:2)
at HTMLDocument.<anonymous> (jnk.html:88)
at e (jquery-3.4.1.min.js:2)
at t (jquery-3.4.1.min.js:2)
Check your JavaScript console - does it show the same error?
If I use an old version of jQuery, 1.7.2, there is no error. This sounds like your problem indeed.
This is the key takeaway: âunrecognized expression: a[href^=#]â. I did a little hunting around, and it seems like â#â needs to be escaped, like this:
$('a[href^=\\#]').bind("click", jump);
Maybe try that small change and see if your required functionality returns.
His formatting suggests heâs embedding this inside a Wordpress page, as it relies on the WP workaround for the redefinition of $ away from the jquery default.
Yes, it works fine using .on(), but now I have found a problem in some browsers in mobile devices. The pages including the script seems to load twice. Maybe the display:none property is added and then removed, Cannot be sure. I described the problem on this other post: