gpt4 book ai didi

javascript - 使用 JavaScript 制作轮播

转载 作者:可可西里 更新时间:2023-11-01 13:46:51 25 4
gpt4 key购买 nike

我正在尝试制作滑动 DIV。

我成功地使第一个 DIV 转到第二个 DIV,反之亦然。但是,我无法将 2nd DIV 滑动到 3rd DIV。

代码如下:

$(function(){

var slideW = $('#slides').width();

// Next slide
$('#next-slide').click(function( e ){
e.preventDefault();
$('#slides').animate({scrollLeft: slideW}, 600);
});

//previous slide
$('#back-slide').click(function( e ){
e.preventDefault();
$('#slides').animate({scrollLeft: -slideW }, 600);
});

});
#slides{  
position:relative;
overflow:hidden;
margin:0 auto;
background:#cf5;
width:100%;
height:200px;
white-space:nowrap;
}
#slides div{
position:relative;
display:inline-block;
margin-right:-4px;
white-space:normal;
vertical-align:top;
*display:inline;
background:#eee;
width:100%;
height:200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="slides">
<div>
Content 1
<a href="#" id="next-slide">Show Content 2 .. it works!</a>
</div>

<div>
Content 2
<a href="#" id="back-slide">Show Content 1 .. it works!</a>
<br>
<a href="#" id="next-slide">Show Content 3 .. not working!!</a>
</div>

<div>
Content 3
<a href="#" id="back-slide">Show Content 2 .. not working!!</a>
</div>

</div>

最佳答案

原生 JavaScript 轮播

使用 CSS display: flex 对齐元素,使用 transitiontransform 进行动画处理。
JS 用于递增/递减 Current Slide 索引并设置父级的 CSS 转换:

const ELS = (sel, EL) => (EL || document).querySelectorAll(sel);
const EL = (sel, EL) => (EL || document).querySelector(sel);

const carousel = (EL_carousel) => {

const EL_mover = EL('.carousel-mover', EL_carousel);
const ELS_slide = ELS('.carousel-slide', EL_carousel);
const ELS_prev = ELS('.prev', EL_carousel);
const ELS_next = ELS('.next', EL_carousel);
const tot = ELS_slide.length;
let c = 0; // Current slide index

const anim = () => {
EL_mover.style.transform = `translateX(-${c * 100}%)`;
};

const prev = () => {
c -= 1;
if (c < 0) c = tot - 1;
anim();
};

const next = () => {
c += 1;
if (c > tot - 1) c = 0;
anim();
};

ELS_prev.forEach(el => el.addEventListener("click", prev));
ELS_next.forEach(el => el.addEventListener("click", next));

};

// Init carousel
ELS(".carousel").forEach(carousel);
.carousel {
position: relative;
overflow: hidden;
}

.carousel-mover {
display: flex;
background: #eee;
transition: transform 0.5s;
}

.carousel-slide {
flex: 1 0 100%;
min-height: 100px;
background: #eee;
}
<div class="carousel">
<div class="carousel-mover">
<div class="carousel-slide">
Page 1 Home
<button type="button" class="next">NEXT</button>
</div>
<div class="carousel-slide">
Page 2
<button type="button" class="prev">PREV</button>
<button type="button" class="next">NEXT</button>
</div>
<div class="carousel-slide">
Page 3 End
<button type="button" class="prev">PREV</button>
<button type="button" class="next">GO HOME</button>
</div>
</div>
</div>


使用 jQuery 和您的代码的示例

另一个使用 jQuery 的不太好的例子,你的命名(因为使用了 ID)和标记有点死板:

$(function(){

var $slide = $("#slides"); // Cache the elements you plan to reuse!
var $pages = $slide.children(); // Get the actual slides pages
var slideW = $slide.width();
var c = 0; // Use a counter

// Use classes instead of ID!
$('.prev, .next').click(function( e ){
c = $(this).is(".next") ? ++c : --c; // incr/decr. counter
$slide.animate({scrollLeft: slideW * c }, 600); // Anim by multiplying
});

});
#slides{  
position:relative;
overflow:hidden;
margin:0 auto;
background:#cf5;
width:100%;
height:200px;
white-space:nowrap;
}
#slides div{
position:relative;
display:inline-block;
margin-right:-4px;
white-space:normal;
vertical-align:top;
*display:inline;
background:#eee;
width:100%;
height:200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="slides">
<div>
Content 1
<a href="#" class="next">Show Content 2</a>
</div>

<div>
Content 2
<a href="#" class="prev">Show Content 1</a>
<br>
<a href="#" class="next">Show Content 3</a>
</div>

<div>
Content 3
<a href="#" class="prev">Show Content 2</a>
</div>
</div>

回顾一下:
上面的代码使用了一个变量 c,而不是总是以固定的 +- 宽度设置动画 slideW,它在每次上一次/下一次点击时分别递增/递减。乘以幻灯片宽度,您将获得 scrollLeft 位置,或者,如果您使用 CSS,则为 transform: translateX 百分比。

关于javascript - 使用 JavaScript 制作轮播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41181555/

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