我試圖做一個horisontal圖像滑塊鼠標懸停 - 我如何觸發多次?
當我做了鼠標懸停只會做.scrollLeft
/一旦觸發功能和移動圖像1px的預計,但我怎麼讓它只要運行的鼠標在鼠標懸停事件上?
樣品點here
回答樣本here
HTML
<div class="flex">
<div id="slideLeft" class="center-c slideLeft"><div class="left"></div></div>
<div id="slideRight" clasS="center-c slideRight"><div class="right"></div></div>
<div id="imagesWrapper" class="imagesWrapper flex">
<img src="img/1.jpg">
<img src="img/2.jpg">
<img src="img/3.jpg">
<img src="img/4.jpg">
<img src="img/5.jpg">
<img src="img/6.jpg">
<img src="img/7.jpg">
<img src="img/8.jpg">
<img src="img/9.jpg">
<img src="img/10.jpg">
<img src="img/11.jpg">
<img src="img/12.jpg">
<img src="img/13.jpg">
</div>
</div>
的Javascript
//IMAGE SCROLL
var slideLeft = document.getElementById('slideLeft');
var slideRight = document.getElementById('slideRight');
var imagesWrapper = document.getElementById('imagesWrapper');
slideLeft.addEventListener('mouseover', profileMouseOverLeft, false);
slideRight.addEventListener('mouseover', profileMouseOverRight, false);
function profileMouseOverLeft() {
imagesWrapper.scrollLeft += -1;
// profileMouseOverLeft();
}
function profileMouseOverRight() {
imagesWrapper.scrollLeft += 1;
// profileMouseOverRight();
}
如果我調用的函數內部它只是直奔終點immidietely
如果我改變功能,這一點,他們只是永遠運行下去......
function profileMouseOverLeft() {
// alert("Hi");
imagesWrapper.scrollLeft += -1;
// profileMouseOverLeft();
setInterval(function(){
alert("Hello");
profileMouseOverLeft();
}, 100);
}
function profileMouseOverRight() {
// alert("Hello");
imagesWrapper.scrollLeft += 1;
setInterval(function(){
profileMouseOverRight();
}, 100);
// profileMouseOverRight();
}
編輯最後JS看起來像這樣和工作得很好
//IMAGE SCROLL
var slideLeft = document.getElementById('slideLeft');
var slideRight = document.getElementById('slideRight');
var imagesWrapper = document.getElementById('imagesWrapper');
var profileRightInterval;
var profileLeftInterval;
slideLeft.addEventListener('mouseover', profileMouseOverLeft, false);
slideRight.addEventListener('mouseover', profileMouseOverRight, false);
function profileMouseOverLeft() {
profileLeftInterval = setInterval(function() {
profileMoveLeft();
}, 25);
}
function profileMouseOverRight() {
// alert("Musen er nu inde");
profileRightInterval = setInterval(function() {
profileMoveRight();
}, 25);
}
function profileMoveLeft(){
imagesWrapper.scrollLeft += -10;
}
function profileMoveRight(){
imagesWrapper.scrollLeft += 10;
}
slideLeft.addEventListener('mouseout', function (e) {
clearInterval(profileLeftInterval);
});
slideRight.addEventListener('mouseout', function (e) {
clearInterval(profileRightInterval);
});
它似乎只是堆疊的1px和滾動一路當我做一個setInterval – Javaish
我設法做一些非常similair到你的建議:) – Javaish
很高興我能幫助:) – SamB