简体   繁体   English

平滑滚动到另一个HTML页面中的div

[英]smooth scrolling to a div in a different html page

I have a menu bar in each of the pages and when I click on one of the sub-items I want the page to redirect to the other html and scroll smoothly to that specific div. 我在每个页面中都有一个菜单栏,当我单击一个子项目时,我希望页面重定向到另一个html并平滑滚动到该特定div。 I am using this code to have it scroll smoothly within the divs of one page: 我正在使用此代码使它在一页的div中平滑滚动:

$(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;
      }
    }
  });
});

Is there a way to modify this code so that I am able to what I am asking? 有没有一种方法可以修改此代码,以便能够满足我的要求?

Use anchor tags and this: 使用定位标记,这是:

$(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;
  }
}
});
});

see this: view-source: http://css-tricks.com/examples/SmoothPageScroll/ 看到这个:查看源代码: http : //css-tricks.com/examples/SmoothPageScroll/

I found simething for the catching page 我发现捕捉页面有很多东西

var match = location.hash.match(/^#?(.*)$/)[1];
if (match)
{
   //call you smooth scroll code. Fake the link click etc
}

While searching for a solution, via google and stackoverflow, to handle smoothscrolling to anchors it was quite apparent that many same-page solutions exist. 在通过google和stackoverflow搜索解决方案以处理平滑滚动以锚定的过程中,很明显存在许多相同页面的解决方案。 Dealing with smoothscroll between multiple anchors across multiple pages seems to be limited to just one QA on stackoverflow. 处理跨多个页面的多个锚点之间的平滑滚动似乎仅限于stackoverflow上的一次质量检查。 That solution as presented however didn't quite work for me. 但是,所提出的解决方案对我而言并不奏效。

While I am just below beginner in handling java and CSS, I would like to pass on a solution I concocted by combining several solutions into one which is at least working on Firefox and Chrome (untested on other browsers). 虽然我在处理Java和CSS方面还不如初学者,但我想传递一种我构想的解决方案,将几种解决方案组合在一起,至少可以在Firefox和Chrome上使用(在其他浏览器上尚未使用)。

I am hoping that someone might have a look at this and offer some suggestions to: 1) make it more cross-browser compatible 2) clean it up 我希望有人可能对此有所了解并提出以下建议:1)使它与跨浏览器更兼容2)清理它

Finally, here are some of the conditions I have it working with without issue: 最后,这是我可以正常使用的一些条件:

Multiple pages - Multiple anchors Bootstrap 3 multiple jquery functions 多个页面-多个锚点Bootstrap 3多个jquery函数

There is on caveat though which I have posted here with regards to Masonry and multiple anchors across pages: Anchors to pages with Masonry layouts 我在这里发布了有关砌体和跨页面的多个锚点的警告:关于带有砌体布局的页面的锚点

    $(function() {
    $('[data-toggle="elementscroll"]').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 -57 //head space
                }, 1000); //scroll speed
                return false;
            }
        }
    });
});

    $(function() {
$('html, body').hide();
if (window.location.hash) {
        setTimeout(function() {
                $('html, body').scrollTop(0).show();
                $('html, body').animate({
                        scrollTop: $(window.location.hash).offset().top -57 // head space
                        }, 1000) //scrollspeed
        }, 0);
}
else {
        $('html, body').show();
}   
    });

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

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