2

当用户翻转缩略图时,我正在尝试使用图像更改制作 js 画廊。我已经成功完成并使用css3淡入动画进行图像更改,它工作正常,但如果我将光标悬停在thumb1上,不要等待动画完成并在另一个缩略图上悬停,图像会在没有动画的情况下更改。很难解释,因为我的英语不是最好的,所以我会给你我的代码。

    <?xml version="1.0" encoding="utf-8" standalone="no"?>
<?xml-stylesheet type="text/css" href="#css21" media="screen"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="xhtml10S" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<meta http-equiv="Window-target" content="_top" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<title>Untitled-1</title>
<style id="css21" type="text/css" media="screen">
html, body {
color : #405060;
font : normal normal normal 95%/1.5 Verdana, Tahoma, Arial, san-serif;
height : auto;
margin : 0;
padding : 0;
text-align : center;
width : auto; }
body {
background-color : #000000; }
div {
border : none;
margin : 0;
padding : 0; }
div#gscreen {
float : none;
height : 250px;
width : 100%;
clear : both;

-webkit-transition: background 1500ms ease-in 0ms; /* property duration timing-function delay */
-moz-transition: background 1500ms ease-in 0ms;
-o-transition: background 1500ms ease-in 0ms;
transition: background 1500ms ease-in 0ms;  }
</style>
<script type="text/javascript">


var imageGallery = ( function() {

var xelem = ( function( xTag ) {
var ua = ((( ie = document.all.tags( xTag )) && !!!( ff = document.getElementsByTagName( xTag ))) ? 1 : 0 );
var xObj = null || { 0 : ff, 1 : ie }[ ua ];
return xObj;
} );
return function imageGallery( tagX ) {
var xItem = xelem("div");
var xScreen = xItem["gscreen"];
var xImg = xelem( tagX );
var iLen = xImg.length;
for ( i = 0; !!( xImg[ i ] ); i++ ) {
xImg[ i ].onmouseover = function() {
var xIndex = Number( this.id.match(/\d+/)[ 0 ] );
xScreen.style.background = " url(" + this.src + ") no-repeat top center";

}
}
}
} )();
onload = function() {
imageGallery("img")
}
</script>
</head>
<body>
<div id="gscreen"></div>

<img id="i0" src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSMMlTzBMWSeD6IrQCLTT7urbpXORjY_2vqmA_mFj6OJcsfsH-yuw" height="100" width="200" style="background-image='url(https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSMMlTzBMWSeD6IrQCLTT7urbpXORjY_2vqmA_mFj6OJcsfsH-yuw)'" alt="test images" />
<img id="i1" src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSlGvpoCsCgGcX74IWG5YNmjULh1Q428frxtmabTCNM_aVKFGH9" style="background-image='url(https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSMMlTzBMWSeD6IrQCLTT7urbpXORjY_2vqmA_mFj6OJcsfsH-yuw)'" height="100" width="200" alt="test images" />
<img id="i2" src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQ3DluQXJW-4stVrHlArRc6gZnuxqmKKyheZsDWt-kGjvfjj43M" height="100" width="200" style="background-image='url(https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSMMlTzBMWSeD6IrQCLTT7urbpXORjY_2vqmA_mFj6OJcsfsH-yuw)'" alt="test images" />
<img id="i3" src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQDC56QxLfOgZyrX96BfPveif-dKlrDIwsB45uGlL3XicDL10EqlA" height="100" width="200" style="background-image='url(https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSMMlTzBMWSeD6IrQCLTT7urbpXORjY_2vqmA_mFj6OJcsfsH-yuw)'" alt="test images" />

</body>
</html>

所以,我想如果我在执行“onmouseover”后暂停 javascript 1500 毫秒,用户将无法在翻转另一个缩略图时再次执行该功能,我该怎么做?或者也许有更多选项来修复图像更改效果?

谢谢您的回答。

4

2 回答 2

0

您可以使用setTimeout 功能

例子:

// run this code after 1500ms
setTimeout(function () {
   // this code will run later;
   alert("Hello World");
},1500);

更新:在你的情况下

xImg[ i ].onmouseover = function() {
    var that = this;
    setTimeout(function () {
        var xIndex = Number( that.id.match(/\d+/)[ 0 ] );
        xScreen.style.background = " url(" + that.src + ") no-repeat top center";
    },1500);
}
于 2012-11-06T19:20:01.243 回答
0

我会给imageGallery一个属性,比如imageGallery.isReady = true;当函数启动时,将属性设置为false. 然后,在你的功能结束时,做setTimeout(function() {imageGallery.isReady = true;}, 1500);

当函数被调用时,只要检查如果imageGallery.isReady = true;不是,就不要执行函数;如果是,执行它。

编辑:或者,您可以在函数启动时删除事件侦听器,然后在之后再次添加它setTimeout();

于 2012-11-06T19:22:56.477 回答