【问题标题】:Slider overflow responsive滑块溢出响应
【发布时间】:2015-05-13 21:37:30
【问题描述】:

我正在使用这个滑块,一切都很好,但为什么在小屏幕上 (小于 480)或在响应视图上它不运行?! 我该如何解决这个问题?!

更多细节在下面的小提琴中

http://jsfiddle.net/roXon/tMxp5/1/

  $(function() {
   var c = 1, 
       timeOut,
       fit,
       isStopped = false, 
       boxw = $('.box').outerWidth(true),
       boxh = $('.box').outerHeight(true),
       boxn = $('.box').length;

    $('#slider').width(boxw*boxn);
    $('#gal, #slider').height(boxh);
    //////////////////////////////////
    function measure() {
        var winw = $(window).width();
        fit = Math.ceil(winw/boxw)-1; // math ceil -1 to get the number of COMPLETELY visible boxes
        $('#gal').width(winw);

        $('#t').html('Boxw='+boxw+' Boxh='+boxh+' Winw='+winw+' VisibleBoxes='+ fit);
    }
    measure();

    $(window).resize(function() {
        measure();
    });
    //////////////////////////////////    
    function b(){
       cc = (c === 1) ? $('.prev').hide() : $('.prev').show();
       ccc =(c >= boxn/fit) ? $('.next').hide() : $('.next').show();    
    }    
    $('.btn').hide();
    /////////////////////////////////

    function a(cb){ 
       $('#slider').animate({left: '-'+ (boxw*fit)*(c-1) },800, cb);
    }
    ////////////////////////////////
    function auto() {
    if(isStopped){ return };
       clearTimeout(timeOut);  
       timeOut = setTimeout(function() {
       $('.btn').hide();            
       c++;             
       if (c >= (boxn/fit)+1) {
          c = 1;
        }            
          a(function(){
             auto();
          });   
       }, 2000);
    }
    auto();
    /////////////////////////////////////////////////////////////////
        $('.next').click(function() {
           c++;
           b();
           a();
        });
        $('.prev').click(function() {
           c--;
           b();
           a();
        });
    /////////////////////////////////////////////////////////////////
    $('#gal').bind('mouseenter mouseleave', function(e) {    
       ( e.type === 'mouseenter' ) ?
       ( isStopped=true, b(), clearTimeout(timeOut) ) :
       ( isStopped=false, auto() );
    });

});

谢谢你

【问题讨论】:

    标签: javascript responsive-design slider


    【解决方案1】:

    这是因为你的 measure 函数将可见框的数量设置为 0

    尝试将测量函数更改为:

    function measure() {
        var winw = $(window).width();
        fit = Math.ceil(winw/boxw)-1; // math ceil -1 to get the number of COMPLETELY visible boxes
        $('#gal').width(winw);
    
        if (fit == 0) fit = 1;
        $('#t').html('Boxw='+boxw+' Boxh='+boxh+' Winw='+winw+' VisibleBoxes='+ fit);
    }
    

    它应该可以解决您的问题:http://jsfiddle.net/tMxp5/270/

    【讨论】:

    • 另一个问题...在这种情况下,单个 div 的宽度是 350 px 但是如果每个 div 有不同的宽度,我怎样才能在每个 div 的开头停止幻灯片?!
    • 我认为您可能必须为此使用不同的插件,因为这个插件似乎适用于一种尺寸的盒子滑动动画 - 所有计算都基于相同的盒子尺寸。执行动画的位在此函数中:function a(cb),您需要计算当前框的大小并将动画更改为减去该宽度。也许在你的小提琴中模拟一个例子,然后发布另一个问题
    • 再次感谢!我会用一个新的小提琴发布另一个问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多