简体   繁体   English

解决平滑滚动问题

[英]Fixing Smooth Scroll Issue

A simplified version of what I'm working with: http://jimmehrabbitdesigns.com/scroll.html 我正在使用的内容的简化版本: http : //jimmehrabbitdesigns.com/scroll.html

I got the scrolling to work, however, it doesn't transition from section to section. 我可以使用滚动条,但是滚动条不会在各个部分之间切换。

Example: if you click NUMBER 3, it will scroll to section THREE. 例如:如果您单击NUMBER 3,它将滚动到第三部分。 From there, this is what happens. 从那里开始,这就是发生的情况。
- Clicking NUMBER 2 takes you back to section ONE. -点击“数字2”可返回到第一部分。
- Clicking NUMBER 4 takes you to section TWO. -点击“数字4”可转到“第二部分”。
- Clicking NUMBER 3 again also takes you back to section ONE. -再按一次NUMBER 3也可回到第1节。

This is the same for all the sections. 所有部分都相同。


jQuery code used: 使用的jQuery代码:

$('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) {
            $('#right').animate({ scrollTop: target.offset().top }, 1000);
            return false;
        }
    }
});

I've changed your code a bit. 我对您的代码做了一些更改。 It's more simple and it helps you understanding what is happening. 它更简单,可以帮助您了解正在发生的事情。 I hope it helps you: 我希望它可以帮助您:

/*
1. I've changed the position fixed to position absolute on the #right element, the scroll won't work with position: fixed set
2. Added the on click event on the anchor tag this way I'm getting the href of the current clicked anchor by using $(this) and attr() method 
3. Added the e.preventdefault() to prevent the default action of the anchor element 
4. Doing the smooth scroll using the href got at 2 as id selector
*/

The jQuery code looks like this: jQuery代码如下所示:

$('#left a').on('click', function(e) {
  e.preventDefault();
  var id = $(this).attr('href');
  $('html, body').animate({
    scrollTop: $(id).offset().top
  }, 1000);
});

You can see the full working snippet by clicking the below button: 点击下面的按钮,您可以看到完整的工作片段:

 $('#left a').on('click', function(e) { e.preventDefault(); var id = $(this).attr('href'); $('html, body').animate({ scrollTop: $(id).offset().top }, 1000); }); 
 html { width: 100%; height: 100%; } body { width: 100%; height: 100%; overflow: hidden; } #left { width: 100%; height: 100%; background-color: #FFFFFF; position: fixed; top: 0; left: 0; } #right { width: 50%; height: 100%; background-color: #0000FF; position: absolute; top: 0; right: 0; z-index: 1; } #one { height: 100%; background-color: #FF0000; } #two { height: 100%; background-color: #00FF00; } #three { height: 100%; background-color: #FFFF00; } #four { height: 100%; background-color: #00FFFF; } #five { height: 100%; background-color: #FF00FF; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='container'> <div id="left"> <a href="#one">NUMBER 1</a> <br> <a href="#two">NUMBER 2</a> <br> <a href="#three">NUMBER 3</a> <br> <a href="#four">NUMBER 4</a> <br> <a href="#five">NUMBER 5</a> </div> <div id="right"> <div id="one" width="100%" height="100%">ONE</div> <div id="two" width="100%" height="100%">TWO</div> <div id="three" width="100%" height="100%">THREE</div> <div id="four" width="100%" height="100%">FOUR</div> <div id="five" width="100%" height="100%">FIVE</div> </div> </div> 

Also you can check this JSFIDDLE if you want. 您也可以根据需要检查此JSFIDDLE

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

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