简体   繁体   English

jQuery通过宽度将左右div左右响应

[英]jQuery animate a responsive div right and left by it's width

I have made a slider on a page with left and right arrows. 我在带有左右箭头的页面上做了一个滑块。 When it's viewed on a desktop site, the width is 800px, and on a mobile site it is 230px. 在桌面网站上查看时,宽度为800px,在移动网站上为230px。 There is a left and right button for the slider, I want to be able to move the slides left and right according to their width. 滑块有一个左右按钮,我希望能够根据其宽度左右移动滑块。 ie if it is 800px wide, I want to slide it 800px to the left and right, and similar for when it is only 230px wide. 即如果它是800像素宽,我想将其向左和向右滑动800像素,当它只有230像素宽时也是如此。

Here is my jQuery code: 这是我的jQuery代码:

var calc_width = $('#calc_viewport').width();
$("#calc_next").click(function(){
if($("#calc_container").position().left == -8000 ) 
    {return;}
else { 
    $("#calc_container").animate({
        left: '-=calc_width'
    }, 1000, 'easeOutExpo');
}
});
$("#calc_prev").click(function(){
if($("#calc_container").position().left >= 0 ) 
    {return;}
else { 
    $("#calc_container").animate({
        left: '+=calc_width'
    }, 1000, 'easeOutExpo');
}
});

The first if statements in each of these makes sure it stops at the first and last slide so a user can't keep sliding. 其中每一个的第一个if语句确保它在第一个和最后一个幻灯片上停止,因此用户无法继续滑动。 Currently this doesn't slide at all. 目前,这根本不会滑动。 Can anyone see where I have gone wrong? 谁能看到我哪里出问题了?

Well for one these are wrong: 好吧,这些是错误的:

left: '+=calc_width'

They should be like so: 他们应该像这样:

left: '+=' + calc_width + 'px'

Or else you're trying to set the left of the element to '+=calc_width' instead of something like 100px. 否则,您尝试将元素的左侧设置为“ + = calc_width”,而不是100px之类的内容。

And since you're animating calc_container by it's width - why is calc_width calculated from calc_viewport? 而且由于您要根据calc_container的宽度设置动画-为什么要从calc_viewport计算calc_width? This should probably be: 这可能应该是:

var calc_width = parseInt($('#calc_container').outerWidth());

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

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