繁体   English   中英

如何使用鼠标悬停停止旋转图像?

[英]How do i stop images rotating using mouseover?

我创建了一个旋转图像的功能,现在我想做的是当我使用mouseover命令时停止旋转图像。 这是我必须让图像旋转的js编码

var m = {
Z   : 100,
xm  : 0,
xmm : .25,
ymm : 0,
ym  : 0,
mx  : 0,
nx  : 0,
ny  : 0,
nw  : 0,
nh  : 0,
xR  : 0,
nI  : 0,
scr : 0,
img : 0,

run : function () {
    m.xm += (m.xmm - m.xm) * .1;
    if (m.ym < m.nw * .15) m.ym++;
    m.xR += m.xm;
    for (var i = 0; i < m.nI; i++){
        var A = (i * 360 / m.nI) + m.xR;
        var x = Math.cos(A * (Math.PI / 180));
        var y = Math.sin(A * (Math.PI / 180));
        var a = m.img[i];
        a.style.width  = ''.concat(Math.round(Math.abs(y * m.ym) + y * m.Z), 'px');
        a.style.left   = ''.concat(Math.round((m.nw * .5) + x * ((m.nw * .5) - (m.nw * .05)) - ((Math.abs(y * m.ym) + y * m.Z) * .5)), 'px');
        a.style.height = ''.concat(Math.round(m.ym + y * m.Z), 'px');
        a.style.top    = ''.concat(Math.round((m.nh * .5) - (m.ym * .5) - x * (m.Z * .5) - (m.ymm * y)), 'px');
        a.style.zIndex = 600 + Math.round(y);
        m.setOpacity(a, (y * 50) + 100);
    }
    setTimeout(m.run, 30);
},

我确实没有详细阅读您的代码,但是您可能可以做的是在函数外部设置一个参数,也许是旋转图像的函数的全局参数,将其称为“ rotate”并将其设置为TRUE

然后,在进行实际旋转之前,请检查此“旋转”参数是否设置为TRUE,如果是,则进行旋转。

现在,在onmouseover上,您所要做的就是将“旋转”参数设置为FALSE,然后在setTimeout触发器到期且函数再次启动时将其设为FALSE,则图像不会旋转,因为它在测试中失败了。

另一种方法是将setTimeout设置为仅在未在mousenotover上触发时才触发,因此,如果在鼠标悬停时,请勿设置超时,否则,请设置超时。

这些只是我在阅读代码时想到的两个主意,我想您可以考虑一下并确定它们是否是您想要的解决方案,如果不是,那么我很想知道您的决定。

干杯。

下一个代码只是一个近似值,比生产代码更像是“伪代码”。 无论如何,都可以根据需要进行修改。

var m = {
run: function() {
    this.isRunning = true;
    // when complete the cycle
    this.cycle = true;
},
play: function() {
    this.timeout = setTimeout((function($this){
        if($this.animCheck !== undefined) clearInterval($this.animCheck);
        $this.run();
    })(this), this.frames);
},
pause: function() {
    this.animCheck = setInterval((function($this) {
        // Obviously, you've to pause the animation when the cycle is finished.
        if($this.cycle) clearTimeout($this.timeout);
    })(this), this.frames);
    return !!this.animCheck;
},
init: function() {
    this.frames = 30;
    this.isRunning = true;
    [the element].addEventListener('mouseover', function() {
        if(this.pause()) this.isRunning = false;
    })
    this.play();
}

};

m.init();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM