简体   繁体   English

在div中向上和向下滚动

[英]scroll up and down in div

I have this div box here overflow:auto and I want to use to programmatically scroll to top and bottom. 我有这个div框溢出:auto我想用编程方式滚动到顶部和底部。

<ul class="-cx-PRIVATE-PostInfo__comments -cx-PRIVATE-PostInfo__commentsSidebarVariant" data-reactid=".1.1.0.0.2.1">

//html style
.-cx-PRIVATE-PostInfo__comments {
     margin-left: -24px;
     margin-right: -24px;
     margin-top: -5px;
     padding-left: 24px;
     padding-right: 24px;
     padding-top: 5px;
}
.-cx-PRIVATE-PostInfo__commentsSidebarVariant {
     overflow: auto;
     padding-bottom: 20px;
}
.-cx-PRIVATE-PostInfo__comments {
     flex-grow: 1;
}
ol, ul {
     list-style: outside none none;  
} 

This method works for scrolling down. 这种方法适用于向下滚动。

jse.executeScript("if ($('.-cx-PRIVATE-PostInfo__comments').scrollTop() != $('.-cx-PRIVATE-PostInfo__comments').outerHeight()) {\n" + 
            "$('.-cx-PRIVATE-PostInfo__comments').animate({scrollTop: $('.-cx-PRIVATE-PostInfo__comments')[0].scrollHeight - $('.-cx-PRIVATE-PostInfo__comments')[0].clientHeight}, 1000);}");

But is there a way to write the code into something like below to check if the scrollHeight is equals to clientHeight? 但有没有办法将代码编写成下面的代码来检查scrollHeight是否等于clientHeight? This code below does not work and has no errors. 下面的代码不起作用,没有错误。

    //return number of pixels the content of <div> element is scrolled vertically
    Long scrollH = (Long)jse.executeScript("return $('.-cx-PRIVATE-PostInfo__comments').scrollTop()");

    //return outer height of a window, including scrollbars
    Long clientH = (Long)jse.executeScript("return $('.-cx-PRIVATE-PostInfo__comments').outerHeight()");

    //check if scrollTop is equals to clientHeight
    Boolean check = (Boolean)jse.executeScript("return $('.-cx-PRIVATE-PostInfo__comments').scrollTop() != $('.-cx-PRIVATE-PostInfo__comments').outerHeight()");

    int index = 1;
    if(check){
        while(scrollH.intValue() > 0){
            screenshot.capture();
            jse.executeScript("window.scrollTo(0," + clientH * index + ")");    
            scrollH  = scrollH - clientH;
            try{
                    Thread.sleep(4000);
            } catch (InterruptedException e){
                Thread.currentThread().interrupt();  //set interrupt flag
            }
            index++;
        }
    }     

You're using scrollTo() . 你正在使用scrollTo() I assume you have a reference to this library? 我假设你有这个库的参考? ( http://lions-mark.com/jquery/scrollTo/ ) however, I don't think you need it in this case. http://lions-mark.com/jquery/scrollTo/ )但是,在这种情况下,我认为你不需要它。 You said you wanted to scroll the div, but you're targeting the window (window.scrollTo()). 你说你想滚动div,但你的目标是窗口(window.scrollTo())。 Don't you want to be targeting your div? 你不想成为你的div的目标吗?

$('.-cx-PRIVATE-PostInfo__comments').scrollTop(clientH * index); 

Update 更新

To actually show you something, I've created a jsfiddle: https://jsfiddle.net/4fLyz57q/ 为了向你展示一些东西,我创建了一个jsfiddle: https ://jsfiddle.net/4fLyz57q/

When you actually load it up the first time you will get some alerts and nothing will show on the screen. 当您第一次实际加载它时,您将收到一些警报,屏幕上不会显示任何内容。 So after it's loaded and shown the alerts, click the Run button. 因此,在加载并显示警报后,单击“运行”按钮。 The alerts are only there to show you that the div is infact scrolling. 警报仅用于向您显示div实际上是滚动的。

I've added a div to your ul to act as content so I can push it down. 我已经为你的ul添加了一个div作为内容,所以我可以把它推下来。 I've also set a height on the ul to it will be scrollable. 我还在ul上设置了一个高度,它可以滚动。

Hopefully this will help. 希望这会有所帮助。 I'm sure you won't have too much trouble converting it to what you actually need with the jse. 我相信你不会有太多麻烦将它转换成你真正需要的jse。

var scrollH = $('.-cx-PRIVATE-PostInfo__comments').prop('scrollHeight');
var clientH = $('.-cx-PRIVATE-PostInfo__comments').innerHeight();
var index = 1;

while(scrollH > clientH)
{
    //screenshot.capture();
    $('.-cx-PRIVATE-PostInfo__comments').scrollTop(clientH * index);    
    scrollH  = scrollH - clientH;
    alert(scrollH);
    index++;
}

I've also commented out / removed server code from this and applied them to the js, again, just to get something working. 我还注释掉了/删除了服务器代码并将它们应用到js,再次,只是为了让一些工作。

Your check variable has been removed. 您的检查变量已被删除。 It didn't need to be there as the while loop condition is checking the scroll height is greater than the inner height. 它不需要在那里,因为while循环条件是检查滚动高度是否大于内部高度。

However, as with your example code, this won't capture a screenshot at all if the condition is not met. 但是,与示例代码一样,如果不满足条件,则根本不会捕获屏幕截图。

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

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