所以我有这个JavaScript文件,用图像制作淡化动画。但是,我不知道在哪里设置鼠标点击功能,只有在按下鼠标时才能使用它。
注意:当调用功能时,需要进入clickFade
模式
var slideShow = function(container, time, effect) {
container = document.querySelector(container);
this.images = [];
this.curImage = 0;
if (effect === "fade") {
for (i = 0; i < container.childElementCount; i++) {
this.images.push(container.children[i]);
this.images[i].style.opacity = 0;
}
// Handle going to to the next slide
var nextSlide = function() {
for (var i = 0; i < this.images.length; i++) {
if (i != this.curImage) this.images[i].style.opacity = 0;
}
this.images[this.curImage].style.opacity = 1;
this.curImage++;
if (this.curImage >= this.images.length) {
this.curImage = 0;
}
window.setTimeout(nextSlide.bind(document.getElementById(this)), time);
};
nextSlide.call(this);
} else if (effect === "clickFade") {
for (i = 0; i < container.childElementCount; i++) {
this.images.push(container.children[i]);
this.images[i].style.opacity = 0;
}
// Handle going to to the next slide
var nextSlideClick = function() {
for (var i = 0; i < this.images.length; i++) {
if (i != this.curImage) this.images[i].style.opacity = 0;
}
this.images[this.curImage].style.opacity = 1;
this.curImage++;
if (this.curImage >= this.images.length) {
this.curImage = 0;
}
window.setTimeout(nextSlideClick.bind(document.getElementById(this)), time);
};
nextSlideClick.call(this);
}
};
slideShow(".slideshow", 2000, "clickFade");
h1 {
font-family: 'Montserrat', sans-serif;
}
.slide {
transition: opacity 0.5s;
position: absolute;
top: 1;
}
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<h1>Exatreo.js - Slideshow library</h1>
<div class="slideshow">
<img class="slide" src="https://www.kasandbox.org/programming-images/animals/birds_rainbow-lorakeets.png" alt="Rainbow lorakeets" />
<img class="slide" src="https://www.kasandbox.org/programming-images/animals/butterfly.png" alt="Butterfly" />
<img class="slide" src="https://www.kasandbox.org/programming-images/animals/cat.png" alt="Cat" />
<img class="slide" src="https://www.kasandbox.org/programming-images/animals/crocodiles.png" alt="Crocodiles" />
<img class="slide" src="https://www.kasandbox.org/programming-images/animals/fox.png" alt="Fox" />
</div>
这是CodePen
谢谢!
答案 0 :(得分:0)
不确定这是否是您的意思,但只有在您点击“幻灯片”div后才能使用此代码触发幻灯片:
$('.slideshow').on('click', function () {
slideShow(".slideshow", 2000, "clickFade");
});
你可以添加它(如果你能够使用jQuery),而不是你的最后一行代码。
或者,没有jQuery:
document.getElementsByClassName("slideshow")[0].addEventListener("click", function () {
slideShow(".slideshow", 2000, "clickFade");
});