jQuery滚动到Div [英] jQuery Scroll to Div

查看:102
本文介绍了jQuery滚动到Div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个FAQ页面,并在顶部有按钮跳转到一个类别(它跳转到我用作类别标签的 p >标签,例如。 < p id =general> 为我的一般类别)。
我不想直接跳到这个类别,而想添加一个滚动效果。我想要 http://www.dynamicdrive.com/dynamicindex3/scrolltop.htm 滚动到我的页面的所需部分。该链接是一个脚本,可以很好地滚动到页面的顶部。我需要类似的东西来滚动到我链接的地方。例如,如果我想去混杂。类别中,我希望能够使< a href =#misc>其他< / a> 并让它滚动到页面的该部分。

解决方案

  $(function(){
$('a [href点击(函数(){
if(location.pathname.replace(/ ^ \ //,'')== this.pathname。* =#]:not([href =#])'替换(/ ^ \ //,'')&& location.hostname == this.hostname){
var target = $(this.hash);
target = target.length? target:$('[name ='+ this.hash.slice(1)+']');
if(target.length){
$('html,body')。animate {
scrollTop:target.offset()。top
},1000);
return false;
}
}
});
});

查看此链接: http://css-tricks.com/snippets/jquery/smooth-scrolling/ 的演示,我以前使用它,它工作得很好。


I am making an FAQ page and have buttons across the top to jump to a category (it jumps to the p tag that I use as the category label, ex. <p id="general"> for my general category). Instead of just jumping right to the category, I want to add a scroll effect. I want something like http://www.dynamicdrive.com/dynamicindex3/scrolltop.htm that scrolls to the desired part of my page. That link is a script that goes to the top of the page with a nice scrolling effect. I need something similar that will scroll to where I link to. For example, if I want to go to a misc. category, I want to just be able to have <a href="#misc">Miscellaneous</a> and have it scroll to that section of the page.

解决方案

$(function() {
  $('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html,body').animate({
          scrollTop: target.offset().top
        }, 1000);
        return false;
      }
    }
  });
});

Check this link: http://css-tricks.com/snippets/jquery/smooth-scrolling/ for a demo, I've used it before and it works quite nicely.

这篇关于jQuery滚动到Div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆