gpt4 book ai didi

javascript - Jquery滚动缩略图图像动画无法正常工作

转载 作者:行者123 更新时间:2023-12-03 03:30:19 26 4
gpt4 key购买 nike

这与我发布的上一个问题 How to make jQuery animate work only once? 有关。现在,我已经成功添加了提供的解决方案,并且它一开始似乎有效。所以我尝试点击下一步按钮,然后如果到达第三次点击,jquery滚动动画就会生效。但是当我尝试单击上一个按钮时,jquery 动画不起作用。它应该回到以前的图像。请帮我解决这个问题。

完整代码如下:

<html>
<head>
<title>TEST</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
<script type="text/javascript" src="Image1.js"></script>
<script type="text/javascript" src="Image2.js"></script>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
</head>
<body>

<img id="defaultPic" src="microsoftLogo1.jpg" /><br/>
<button id= "prev" class="portfolioNavigation">Previous</button>
<div id="container">
<ul id="imageList">
<li><img id="subPic1" src="microsoftLogo1.jpg" /></li>
<li><img id="subPic2" src="microsoftLogo2.jpg" /></li>
<li><img id="subPic3" src="microsoftLogo3.gif" /></li>
<li><img id="subPic4" src="microsoftLogo4.jpg" /></li>
<li><img id="subPic5" src="microsoftLogo5.png" /></li>
<li><img id="subPic6" src="microsoftLogo6.png" /></li>

</ul>
</div>
<button id= "next" class="portfolioNavigation">Next</button>
<script type="text/javascript">
var animation = true;
var counter = 0;
var srcArray = ["microsoftLogo1.jpg", "microsoftLogo2.jpg",
"microsoftLogo3.gif", "microsoftLogo4.jpg", "microsoftLogo5.png",
"microsoftLogo6.png"];
var numImages = srcArray.length;

if(counter == 0){
document.getElementById('prev').disabled = 'true';
document.getElementById('prev').style.opacity = '0.5';
}

prev.onclick = function(){

document.getElementById("defaultPic").src = srcArray[(counter - 1) %
numImages];
counter--;

if(counter == 0){
document.getElementById('prev').disabled = true;
document.getElementById('prev').style.opacity = '0.5';
document.getElementById('next').disabled = true;
document.getElementById('next').style.opacity = '0.5';
}

if(counter == 2){
$(function() {
$('#prev').on('click', function() {
if (animation) {

$('#imageList').stop().animate({
left: '+=285px'
}, 1000, function() {
animation = false;
});
}
});
});
}
};

next.onclick = function() {

document.getElementById("defaultPic").src = srcArray[(counter + 1) %
numImages];
counter++;

if (counter == 5) {
document.getElementById('next').disabled = true;
document.getElementById('next').style.opacity = '0.5';
}

if (counter == 2) {
document.getElementById('prev').disabled = false;
document.getElementById('prev').style.opacity = '1';
$(function() {
$('#next').on('click', function() {
if (animation) {

$('#imageList').stop().animate({
left: '-=285px'
}, 1000, function() {
animation = false;
});
}
});
});
}
};


</script>
</body>

最佳答案

您有一个变量 animate,您试图用它来控制两个单独的动画,只需设置另一个变量并以与第一个变量相同的方式使用它...

<html>
<head>
<title>TEST</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
<script type="text/javascript" src="Image1.js"></script>
<script type="text/javascript" src="Image2.js"></script>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
</head>
<body>

<img id="defaultPic" src="microsoftLogo1.jpg" /><br/>
<button id= "prev" class="portfolioNavigation">Previous</button>
<div id="container">
<ul id="imageList">
<li><img id="subPic1" src="microsoftLogo1.jpg" /></li>
<li><img id="subPic2" src="microsoftLogo2.jpg" /></li>
<li><img id="subPic3" src="microsoftLogo3.gif" /></li>
<li><img id="subPic4" src="microsoftLogo4.jpg" /></li>
<li><img id="subPic5" src="microsoftLogo5.png" /></li>
<li><img id="subPic6" src="microsoftLogo6.png" /></li>

</ul>
</div>
<button id= "next" class="portfolioNavigation">Next</button>
<script type="text/javascript">
var animation = true;
var backAnimation = true;
var counter = 0;
var srcArray = ["microsoftLogo1.jpg", "microsoftLogo2.jpg",
"microsoftLogo3.gif", "microsoftLogo4.jpg", "microsoftLogo5.png",
"microsoftLogo6.png"];
var numImages = srcArray.length;

if(counter == 0){
document.getElementById('prev').disabled = 'true';
document.getElementById('prev').style.opacity = '0.5';
}

prev.onclick = function(){

document.getElementById("defaultPic").src = srcArray[(counter - 1) %
numImages];
counter--;

if(counter == 0){
document.getElementById('prev').disabled = true;
document.getElementById('prev').style.opacity = '0.5';
document.getElementById('next').disabled = true;
document.getElementById('next').style.opacity = '0.5';
}

if(counter == 2){
$(function() {
$('#prev').on('click', function() {
if (backAnimation) {

$('#imageList').stop().animate({
left: '+=285px'
}, 1000, function() {
backAnimation = false;
});
}
});
});
}
};

next.onclick = function() {

document.getElementById("defaultPic").src = srcArray[(counter + 1) %
numImages];
counter++;

if (counter == 5) {
document.getElementById('next').disabled = true;
document.getElementById('next').style.opacity = '0.5';
}

if (counter == 2) {
document.getElementById('prev').disabled = false;
document.getElementById('prev').style.opacity = '1';
$(function() {
$('#next').on('click', function() {
if (animation) {

$('#imageList').stop().animate({
left: '-=285px'
}, 1000, function() {
animation = false;
});
}
});
});
}
};


</script>
</body>

关于javascript - Jquery滚动缩略图图像动画无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46131984/

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