简体   繁体   English

滚动溢出的div

[英]Scroll inside an overflowed div

I am new to javascript and at the moment I am trying to learn more about it. 我是javascript的新手,目前我正在尝试更多地了解它。 My idea was to create a simple navigation menu plus a div which overflow is enabled and has many divs inside it. 我的想法是创建一个简单的导航菜单加上一个div,它启用溢出并且里面有很多div。 However, it seems that the code that I am using for the scrolling inside the overflowed div isn't working. 但是,似乎我用于在溢出的div内滚动的代码不起作用。 I tried to fix the problem by myself but my knowledge wasn't enough. 我试图自己解决问题,但我的知识还不够。

At the bottom I will give a running sample of the the code I wrote. 在底部,我将给出我编写的代码的运行示例。 The problems are when you click on link "Page 2" the div is actually scrolling down to the second div. 问题是当你点击链接“第2页”时,div实际上是向下滚动到第二个div。 However, if you press on the link "Page 2" while you are already on this page, you will go automatically to the first div. 但是,如果您已经在此页面上时按下链接“第2页”,您将自动转到第一个div。 Also when we are on "Page 2" the link for "Page 3" seems not to work. 此外,当我们处于“第2页”时,“第3页”的链接似乎无效。 If someone could help me or at least give me some suggestions how to fix this problem I will be really grateful. 如果有人可以帮助我或至少给我一些建议如何解决这个问题,我将非常感激。 Thank you in advance. 先感谢您。

 $(".Link1").click(function() { $('.Box').animate({ scrollTop: $(".Page1").offset().top}, 'slow'); }); $(".Link2").click(function() { $('.Box').animate({ scrollTop: $(".Page2").offset().top}, 'slow'); }); $(".Link3").click(function() { $('.Box').animate({ scrollTop: $(".Page3").offset().top}, 'slow'); }); 
 body{ margin:0; padding:0; } .Nav{ position:relative; width:10%; height:100vh; background-color:red; float:left; } .Link1{ position:relative; color:#FFFFFF; text-align:center; font-size:3vw; cursor: pointer; } .Link2{ position:relative; color:#FFFFFF; text-align:center; font-size:3vw; cursor: pointer; } .Link3{ position:relative; color:#FFFFFF; text-align:center; font-size:3vw; cursor: pointer; } .Box{ position:relative; width:90%; height:100vh; background-color:green; overflow-y:hidden; float:left; } .Page1{ position:relative; width:100%; height:100vh; background-color:green; } .Page2{ position:relative; width:100%; height:100vh; background-color:gold; } .Page3{ position:relative; width:100%; height:100vh; background-color:gold; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="Nav"> <div class="Link1"> Page 1 </div> <div class="Link2"> Page 2 </div> <div class="Link3"> Page 3 </div> </div> <div class="Box"> <div class="Page1"> Page 1 </div> <div class="Page2"> Page 2 </div> <div class="Page3"> Page 3 </div> </div> 

Let me suggest you the best option for anchor scrolling: 让我建议你最好的锚滚动选项:

$(document).on('click', '.scroller-link', function () {
    var obj = $($(this).attr('href'));

    if (obj.size() > 0) {
        $(selector).animate({
            scrollTop: obj.offset().top
        });
        return false;
    }
});

(don't forget to set selector ) (别忘了设置selector

HTML: HTML:

<a class="scroller-link" href="#divID"></a>
<div id="divID"></div>

as per your code, you need to define variable before click event, but for implementation @Undefitied answer is best option. 根据你的代码,你需要在click事件之前定义变量,但是为了实现@Undefitied的答案是最好的选择。

var var1 = $(".Page1").offset().top;
var var2 = $(".Page2").offset().top;
var var3 = $(".Page3").offset().top;

$(".Link1").click(function() {
    $('.Box').animate({
        scrollTop: var1},
        'slow');
});

$(".Link2").click(function() {
    $('.Box').animate({
        scrollTop: var2},
        'slow');
});


$(".Link3").click(function() {
    $('.Box').animate({
        scrollTop: var3},
        'slow');
});

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

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