gpt4 book ai didi

javascript - 自动播放 Javascript 轮播,直到单击按钮

转载 作者:行者123 更新时间:2023-11-28 02:31:00 24 4
gpt4 key购买 nike

所以我有一个整洁的小旋转木马,可以通过单击向左或向右箭头来运行。

Javascript:

unitWidth = 760;
unitTotal = 4;
unitCtr = 1;

onLeftArrow = function(e) {
//alert("Left");
disableArrows();
if (unitCtr<=unitTotal) {
unitCtr++;
TweenLite.to(productImg, 0.6, {x: "-="+unitWidth, onComplete:enableArrows });
}
hideArrows();
}

onRightArrow = function(e) {
//alert("Right");
disableArrows();
if (unitCtr>1) {
unitCtr--;
TweenLite.to(productImg, 0.6, {x: "+="+unitWidth, onComplete:enableArrows });
}
hideArrows();
}

function hideArrows() {
//alert(unitCtr)
if (unitCtr <= 1) {
arrowRight.style.visibility = "hidden";
arrowLeft.style.visibility = "visible";
}
if (unitCtr >= unitTotal) {
arrowRight.style.visibility = "visible";
arrowLeft.style.visibility = "hidden";
}
if (unitCtr>1 && unitCtr<unitTotal) {
arrowRight.style.visibility = "visible";
arrowLeft.style.visibility = "visible";
}
}

function disableArrows() { //ADDED NEW FUNCTION TO DISABLE ARROWS
arrowLeft.removeEventListener('click', onLeftArrow, false);
arrowRight.removeEventListener('click', onRightArrow, false);
}

function enableArrows() { //ADDED NEW FUNCTION TO RE-ENABLE ARROWS
arrowLeft.addEventListener('click', onLeftArrow, false);
arrowRight.addEventListener('click', onRightArrow, false);
}

HTML:

<div id="arrowL">
<img src="arrow_click.png" width="100" height="415" />
</div>

<div id="arrowR">
<img src="arrow_click.png" width="100" height="415" />
</div>

<div id="product_img">
<div class="img_container" id="product1">
<img src="panel1.jpg" class="product" />
</div>
<div class="img_container" id="product2">
<img src="panel2.jpg" class="product" />
</div>
<div class="img_container" id="product3">
<img src="panel3.jpg" class="product" />
</div>
<div class="img_container" id="product4">
<img src="panel4.jpg" class="product" />
</div>
</div>

我希望轮播一直自动播放一次,直到播放结束或有人点击箭头。有关执行此操作的最佳方法的任何建议? (我正在使用 GSAP 来处理实际运动)

最佳答案

尝试将处理转换的部分代码用作模块,以便更轻松地调用/使用它们。

请注意:我没有测试它,但它会让您了解轮播应该如何作为自动播放工作以及如何停止它。 (您也可以添加其余代码,这只是关于新的或更新的部分。)

javascript:

var unitWidth = 760,
unitTotal = 4,
unitCtr = 1,
interval;

onLeftArrow = function(e) {
disableArrows();
transition_forward();
hideArrows();
}

function transition_forward(){
if (unitCtr<=unitTotal) {
unitCtr++;
TweenLite.to(productImg, 0.6, {x: "-="+unitWidth, onComplete:enableArrows });
}
}

function auto_play(){
interval = setInterval(function(){
transition_forward();
}, 2500);
}

auto_play();

clearIV = function(){
clearInterVal(interval );
}

arrowLeft.addEventListener('click', clearIV, false);
arrowRight.addEventListener('click', clearIV, false);

关于javascript - 自动播放 Javascript 轮播,直到单击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47705833/

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