jquery smoothscroll不适用于chrome

时间:2018-05-20 14:26:20

标签: jquery html smooth-scrolling

我有一个JQuery事件监听器,它应该阻止使用import collections dictionary = collections.defaultdict(int) setlimit = 10 #int(input("Please enter limit for N. Then press enter:")) parameter = set(["RY", "TZ"]) with open("C:/file.txt", "rt") as f: for line in f: user, identify = line.split(maxsplit=2)[:2] if identify not in parameter: dictionary[user] += 1 dictionary = dict(dictionary) print(dictionary) 类的超链接上的默认“跳转到div”函数,而是将文档的滚动设置为div。

它在Safari上工作正常但由于某种原因它在Chrome上没有任何作用。它可以防止文档直接跳转到元素,但除此之外不会执行任何操作。我已经检查过单击超链接时调用的函数,但我不知道为什么它没有动画。

我的超链接:

.smoothscroll

我的JQuery功能:

<a class="smoothscroll" href="#slide2">GO</a>

如果这是任何帮助,$(function(){ $('.smoothscroll').click(function(){ var id = $(this).attr('href'); console.log($(id).offset().top); console.log(document.documentElement.scrollTop); $('html, body').animate({scrollTop: $(id).offset().top}, 'slow'); return false; }); }); 确实在px中返回正确的值以滚动到。

2 个答案:

答案 0 :(得分:0)

您好我在我的Chrome浏览器上尝试了您的代码并且运行良好。您只需要将id="slide2"放到要滚动到的div上即可。尝试删除浏览器的缓存。

答案 1 :(得分:0)

你可以在这里使用吗?

jquery代码

$(document).ready(function(){
$("a").on("click", function(event) {

// Make sure this.hash has a value before overriding default behavior
if (this.hash !== "") {
  // Prevent default anchor click behavior
  event.preventDefault();

  // Store hash
  var hash = this.hash;

  // Using jQuerys animate() method to add smooth page scroll
  // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
  $("html, body").animate({
    scrollTop: $(hash).offset().top
  }, 1600, function(){

    // Add hash (#) to URL when done scrolling (default click behavior)
    window.location.hash = hash;
  });
}
});
});