gpt4 book ai didi

javascript - 给 jQuery 内容 slider 百分比宽度

转载 作者:行者123 更新时间:2023-11-28 18:10:56 24 4
gpt4 key购买 nike

我正在使用 Lightweight jQuery Content Slider并试图找出如何使宽度基于百分比而不是基于像素以适应响应式设计。这是 slider 的代码:

HTML

<div id="button">
<a class="button1 active" rel="1" href="#"></a>
<a class="button2" rel="2" href="#"></a>
<a class="button3" rel="3" href="#"></a>
</div> <!-- end of div button-->

<div class="clear"></div>

<div id="myslide">
<div class="cover">

<div class="mystuff">
some slide content
</div>
<div class="mystuff">
some slide content
</div>
<div class="mystuff">
some slide content
</div>

</div> <!-- end of div cover -->
</div> <!-- end of div myslide -->

CSS

#myslide {width:160px;overflow:hidden;position: relative;height:170px;margin-bottom:20px}

#myslide .cover{
width:480px; /*------- class mystuff width * number of mystuff divs (160 * 3 = 480)---------- */
position: absolute;
height:160px;
}

#myslide .mystuff {width:160px;float:left;padding:20px 0;}

.button1,.button2,.button3{background:#999;padding:6px;display:block;float:left;margin-right:5px;}
.active{background:#111;padding:6px;display:block;float:left;outline:none;}
.clear{clear:both;}

JS

$(document).ready(function (){
$('#button a').click(function(){
var integer = $(this).attr('rel');
$('#myslide .cover').animate({left:-160*(parseInt(integer)-1)}) /*----- Width of div mystuff (here 160) ------ */
$('#button a').each(function(){
$(this).removeClass('active');
if($(this).hasClass('button'+integer)){
$(this).addClass('active')}
});
});
});

最佳答案

这里和那里的一些小调整。

**先替换

left:-160*(parseInt(integer)-1)

left: -($('#myslide').width()) * (parseInt(integer) - 1)

然后一些 CSS 更改,例如

#myslide .cover {
width:300%;
}
#myslide .mystuff {
width:33.3%;
}

现在你只需要改变#myslide的宽度到你想要的任何东西

#myslide {
width: 160px;
}

这是工作副本 - http://jsfiddle.net/E2xp4/

关于javascript - 给 jQuery 内容 slider 百分比宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18665244/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com