作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这与我发布的上一个问题 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/
我是一名优秀的程序员,十分优秀!