2

だから私はこのギャラリースクリプトを手に入れましたが、それはうまくいきますが、onMousedown関数をインポートしようとしています.

<html>

<head>

<script type='text/javascript'>


var imageGallery = [
"img1.jpg" ,       
"img2.jpg" ,
"img3.jpg",
"img4.jpg"
];

var imgCount = 0;
var totalImgs = imageGallery.length - 1;

function next() {
imgCount++ ;
if(imgCount > totalImgs) imgCount = 0

document.getElementById("gallery").src = imageGallery[imgCount] ;
}

function previous() {
imgCount--;
if(imgCount < 0) imgCount = totalImgs ;
document.getElementById("gallery").src = imageGallery[imgCount] ;    
} 

</script>   

</head>

<body>

<a href="#" onMousedown="next(); return false;">Next</a>
<a href="#" onMousedown="previous(); return false;">Back</a>
<img id="gallery"src="img1.jpg"style="height:420px;width:744px">   

</body>

</html>
4

1 に答える 1

1

この方法でマウスホールドをフックしてみることができます:

  • mouseDown イベント関数で間隔を設定し、すぐに実行します。インターバル内で前/次のアクションを実行
  • window.onmouseupイベント関数で間隔をクリア

インライン イベント バインディングを削除することを検討してください。

HTML:

<a href="#" onMousedown="next(); return false;">Next</a>

<a href="#" onMousedown="previous(); return false;">Back</a>

<img id="gallery" src="http://placehold.it/350x150" style="height:420px;width:744px">

コード:

var imageGallery = [
    "http://placehold.it/350x150",
    "http://placehold.it/350x250",
    "http://placehold.it/350x350",
    "http://placehold.it/350x450"];

var imgCount = 0;
var totalImgs = imageGallery.length - 1;
var timer;

function next() {
    timer = setInterval(function fn() {
        imgCount++;
        if (imgCount > totalImgs) imgCount = 0

        document.getElementById("gallery").src = imageGallery[imgCount];
        return fn;
    }(), 500)
}

function previous() {
    timer = setInterval(function fn() {
        console.log('prev')
        imgCount--;
        if (imgCount < 0) imgCount = totalImgs;
        document.getElementById("gallery").src = imageGallery[imgCount];
        return fn;
    }(), 500)
}

function stopInterval() {
    clearInterval(timer)
}

window.onmouseup = stopInterval;

デモ: http://jsfiddle.net/T42jj/

于 2013-12-11T14:54:01.800 に答える