简体   繁体   English

如果存在视口,为什么jQuery animate无法在移动设备上运行?

[英]Why does jQuery animate not work on mobile devices if viewport is present?

So the following works fine on my webpage on Desktop but doesn't cause any scroll on Mobile... 因此,以下内容在我的桌面版网页上正常运行,但不会在移动版上引起任何滚动...

 $("HTML, BODY").animate({
        scrollTop: 500
    }, 1000);

This post seems to suggest it has something to do with mobile devices not scrolling on body but on viewport instead. 这篇文章似乎暗示它与移动设备有关,不是在身体上滚动而是在视口上滚动。 And if I remove this viewport tag from my page then the scroll does work.... 而且,如果我从页面中删除此视口标签,则滚动确实起作用。

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

But I have seen pages where the viewport tag is present but the animation works, so what gives? 但是我看过存在视口标签但动画有效的页面,那能给什么呢?

In my case, I'm using a hamburger menu when the windows width is under 930px. 就我而言,当窗口宽度小于930px时,我正在使用汉堡菜单。 Below this limit, the scroll were broken because I needed to make the site-content scroll instead of the body : 在此限制之下,滚动被破坏了,因为我需要使站点内容滚动而不是正文:

var page = $('#myAnchor'); // target page
var speed = 750; // animation (ms)

if ($( window ).width() <= 930 ){
        $('.site-content').animate( { scrollTop: $(page).offset().top }, speed );
}
else {
        $('html, body').animate( { scrollTop: $(page).offset().top }, speed, function(){//calback} ); // Go 
    }

I'm having the same problem as he described. 我遇到了与他描述的问题相同的问题。 I'm using this: 我正在使用这个:

$(".buttonTop").click(function() {
  $('html, body').animate({
      scrollTop: $(".bestline").offset().top},
      1300);
});

And as he described, when removing <meta name="viewport" content="width=device-width, initial-scale=1.0"> 正如他所描述的,删除<meta name="viewport" content="width=device-width, initial-scale=1.0">

Things start working. 事情开始起作用。 And it's not related to mobile device or browser because it happends in Chrome console too. 而且它与移动设备或浏览器无关,因为它也在Chrome控制台中发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM