【问题标题】:jQuery: Stop scrolling text when margin-top < 0jQuery:当 margin-top < 0 时停止滚动文本
【发布时间】:2012-12-19 07:10:12
【问题描述】:

我得到带有 2 个按钮的滚动文本工作正常,但我似乎无法让它停止。如果margin

请在此处查看演示:

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

代码:

$(function() {

    var margin = parseInt(jQuery('#content').css('margin-top'), 10);

    $('#scroll-down').click(function() {

        $('#content').animate({
            'margin-top': '+=50'
        }, 1000);

        return margin;

    });

    if (margin > 0) {
        $('#scroll-up').click(function() {

            $('#content').animate({
                'margin-top': '-=50'
            }, 1000);

        });
    }

});

【问题讨论】:

    标签: jquery if-statement scroll jquery-animate margin


    【解决方案1】:

    您需要在每次点击事件时更新边距。

    $(function () {
        $('#scroll-down').click(function () {
            $('#content').animate({
                'margin-top': '+=50'
            }, 1000);
            return margin;
        });
    
        $('#scroll-up').click(function () {
            var margin = parseInt(jQuery('#content').css('margin-top'), 10);
            if (margin >= 0) {
                $('#content').animate({
                    'margin-top': '-=50'
                }, 1000);
            }
        });
    });
    

    【讨论】:

      【解决方案2】:

      您需要在点击中移动该 if 语句。您还需要在每次单击时更新边距。我也想把 if 语句放在向下点击周围,以确保如果你在顶部,它不能向下移动。

      updated jsbin

      jQuery

      $(function(){
      
         var margin = parseInt(jQuery('#content').css('margin-top'), 10);
      
         $('#scroll-down').click(function(){
             if( margin > 0 ) {
                 // Update margin
                 margin -= 50;
                 $('#content').animate({
                    'margin-top': '+=50'
                 }, 1000);
             }
         });
      
      
         $('#scroll-up').click(function(){
             // Update margin
             margin += 50;
             $('#content').animate({
                'margin-top': '-=50'
             }, 1000);
         });
      });
      

      【讨论】:

      • 非常感谢你们两个!巨大的帮助!
      猜你喜欢
      • 2017-02-13
      • 2013-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-29
      • 1970-01-01
      • 2010-10-25
      相关资源
      最近更新 更多