jQuery scroll a div up and down using two buttons

后端 未结 4 2211
执念已碎
执念已碎 2021-02-10 13:45

I have a simple set of two buttons that when hovered should make a div move up and down to simulate a scrolling effect:

$(\"#down\").hover(function () {

    $(\         


        
4条回答
  •  别那么骄傲
    2021-02-10 14:34

    This code still need some debugging, but you can get the idea in it:

    $(document).ready(function() {
    
        if ($('.content').height() > $('.container').height()) {
            $("#down").hover(function () {
                animateContent("down");
            }, function() { $('.content').stop(); });
    
            $("#up").hover(function () {
                animateContent("up");
            }, function() { $('.content').stop(); });
        }
    });
    
    function animateContent(direction) {  
        var animationOffset = $('.container').height() - $('.content').height();
        if (direction == 'up') {
            animationOffset = 0;
        }
    
        $('.content').animate({ "marginTop": animationOffset + "px" }, "fast");
    }
    

    Code: http://jsfiddle.net/a89DF/6/

提交回复
热议问题