简体   繁体   中英

How to make auto smooth scroll using js

I would like to auto scroll my page using js. Example page with big image:

<html>
<body>
<IMG SRC="some_image.jpg" ALT="some text">
</body>
</html>

I found here how to do it but I don't understand how to use it.

Could some one write me how to use that function?

function pageScroll() {
window.scrollBy(0,1);
scrolldelay = setTimeout('pageScroll()',10);
}

I understand it must be betwen tags but I guess thats not enought

ok small edit:

I found something like this and it works

<script src="jquery.js"></script>
<script>
$(document).ready(function () {
    setInterval(function () {
        var iScroll = $(window).scrollTop();
        iScroll = iScroll + 200;
        $('html, body').animate({
            scrollTop: iScroll
        }, 1000);
    }, 2000);
});
</script>

The problem is I have to scroll my content which is in iframe How to modify this?

edit2:

I change my code as you said now it looks like this:

<html>
<body>

<script src="jquery.js"></script>
<script>
$(document).ready(function () {
setInterval(function () {
    var iScroll = $("myframe")[0].conetentWindow.scrollTop;
    iScroll = iScroll + 200;
    $('html, body', parent.document).animate({
        scrollTop: iScroll
    }, 1000);
}, 2000);
});

document.write('<iframe src="http://link_to_image/image.jpg" name="myframe" height="50%" width="50%"></iframe>')

</script>
</body>
</html>

But it doesn't scroll my frame :/

The problem is I have to scroll my content which is in iframe

$(document).ready(function () {
    setInterval(function () {
        var iScroll = $("#myIframe")[0].conetentWindow.scrollTop;
        iScroll = iScroll + 200;
        $('html, body', parent.document).animate({
            scrollTop: iScroll
        }, 1000);
    }, 2000);
});

After talking to the OP - the iframe is on another doamin ( his control)

so this is one of the solutions :

http://jsbin.com/UyAqIkUF/2/edit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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