Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在锚上平滑滚动_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 在锚上平滑滚动

Javascript 在锚上平滑滚动,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我想在单击导航栏中的按钮时添加平滑滚动效果。 它跳转到链接,但没有平滑滚动效果 我的分区代码: <section id="home"></section> 第二个 $(document).ready(function() { // check for hash when page has loaded if (getHash() != null) { checkForScrolling(); } }); // listen fo

我想在单击导航栏中的按钮时添加平滑滚动效果。 它跳转到链接,但没有平滑滚动效果

我的分区代码:

<section id="home"></section>
第二个

$(document).ready(function() {
    // check for hash when page has loaded
    if (getHash() != null) {
        checkForScrolling();
    }
});

// listen for hash change event here
window.onhashchange = function() {
    checkForScrolling();
};

// return hash if so or null if hash is empty
function getHash() {
    var hash = window.location.hash.replace('#', '');
    if (hash != '') {
        return hash;
    } else {
        return null;
    }
}

// this function handles your scrolling
function checkForScrolling() {
    // first get your element by attribute selector
    var elem = $('[data-anchor="' + getHash() + '"]');

    // cheeck if element exists
    if (elem.length > 0) {
        $('html, body').stop().animate({
            scrollTop: elem.offset().top
        }, 300);
    }
}

有趣的是,这不是一个调试代码的请求,下面是一个


说明是

您放弃了
}后面的结束括号;}//如果是零件,则结束。
所以如果你粘贴这个
};})到第一个底部,它将正常工作

完成的代码如下所示:

$(document).ready(function(){
// Add smooth scrolling to all links
$("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 jQuery's 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
  }, 800, function(){

    // Add hash (#) to URL when done scrolling (default click behavior)
    window.location.hash = hash;
  });
} // End if 
});
});
尝试使用
attr()
获取
id
值,然后将
scrollTop
值设置为特定id
offset()。top
并使用
animate()
平滑滚动
单击

堆栈片段

$(文档)。在(“单击”,“导航链接”,功能(e){
e、 预防默认值();
var hash=$(this.attr(“href”);
if($(散列).length==0){
返回;
}
//console.log($(hash.offset().top)
$('html,body')。设置动画({
scrollTop:$(散列).offset().top
}, 800)
})

其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分 家 其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
其他部分
$(文档).ready(函数(){
//添加平滑滚动到所有链接
$(“a”)。在('mouseover',函数(事件){
//在覆盖默认行为之前,请确保this.hash具有值
如果(this.hash!==“”){
//防止默认锚点单击行为
event.preventDefault();
//存储散列
var hash=this.hash;
//使用jQuery的animate()方法添加平滑页面滚动
//可选数字(800)指定滚动到指定区域所需的毫秒数
$('html,body')。设置动画({
scrollTop:$(散列).offset().top
},800,函数(){
//完成滚动后,将哈希(#)添加到URL(默认单击行为)
window.location.hash=散列;
});
}//如果结束,则结束
});
});
body,html,.main{
身高:100%;
}
部分{
宽度:80%;
身高:80%;
}


第一种方法可能有输入错误?单击功能没有右括号,我添加了右括号。也没有结束括号来准备文档
$(document).ready(function() {
    // check for hash when page has loaded
    if (getHash() != null) {
        checkForScrolling();
    }
});

// listen for hash change event here
window.onhashchange = function() {
    checkForScrolling();
};

// return hash if so or null if hash is empty
function getHash() {
    var hash = window.location.hash.replace('#', '');
    if (hash != '') {
        return hash;
    } else {
        return null;
    }
}

// this function handles your scrolling
function checkForScrolling() {
    // first get your element by attribute selector
    var elem = $('[data-anchor="' + getHash() + '"]');

    // cheeck if element exists
    if (elem.length > 0) {
        $('html, body').stop().animate({
            scrollTop: elem.offset().top
        }, 300);
    }
}
// Select all links with hashes
$('a[href*="#"]')
  // Remove links that don't actually link to anything
  .not('[href="#"]')
  .not('[href="#0"]')
  .click(function(event) {
    // On-page links
    if (
      location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') 
      && 
      location.hostname == this.hostname
    ) {
      // Figure out element to scroll to
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
      // Does a scroll target exist?
      if (target.length) {
        // Only prevent default if animation is actually gonna happen
        event.preventDefault();
        $('html, body').animate({
          scrollTop: target.offset().top
        }, 1000, function() {
          // Callback after animation
          // Must change focus!
          var $target = $(target);
          $target.focus();
          if ($target.is(":focus")) { // Checking if the target was focused
            return false;
          } else {
            $target.attr('tabindex','-1'); // Adding tabindex for elements not focusable
            $target.focus(); // Set focus again
          };
        });
      }
    }
  });
$(document).ready(function(){
// Add smooth scrolling to all links
$("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 jQuery's 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
  }, 800, function(){

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