简体   繁体   English

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

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

I have created a function to rotate images, and now what I would like to do is to stop the images from rotating when I use the mouseover command. 我创建了一个旋转图像的功能,现在我想做的是当我使用mouseover命令时停止旋转图像。 this is the js coding I have to get the images to rotate 这是我必须让图像旋转的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);
},

I really did not read your code in detail, but what you could probably do is set a parameter outside the function, maybe a global parameter to the function that rotates your image, call it "rotate" and set it to TRUE 我确实没有详细阅读您的代码,但是您可能可以做的是在函数外部设置一个参数,也许是旋转图像的函数的全局参数,将其称为“ rotate”并将其设置为TRUE

Then, before you make the actual rotation, you check if this "rotate" param set to TRUE, if yes then rotate. 然后,在进行实际旋转之前,请检查此“旋转”参数是否设置为TRUE,如果是,则进行旋转。

Now, onmouseover, all you have to do is set the "rotate" param to FALSE, and then when it its FALSE when the setTimeout trigger is expires and the function starts again, the imagee would not rotate becaues it failed your test. 现在,在onmouseover上,您所要做的就是将“旋转”参数设置为FALSE,然后在setTimeout触发器到期且函数再次启动时将其设为FALSE,则图像不会旋转,因为它在测试中失败了。

Another approach is to set your setTimeout to trigger only when not on mousenotover, so if on mouse over, do not set the timeout, othersie, set the time out. 另一种方法是将setTimeout设置为仅在未在mousenotover上触发时才触发,因此,如果在鼠标悬停时,请勿设置超时,否则,请设置超时。

Those are just two ideas that just came to my mind reading your code, I think you can think about it and decide if those are solutions you like, and if not then I would be interested to know what you decided. 这些只是我在阅读代码时想到的两个主意,我想您可以考虑一下并确定它们是否是您想要的解决方案,如果不是,那么我很想知道您的决定。

Cheers. 干杯。

The next code is just an approximation, and is more like a "pseudo code" than a production code. 下一个代码只是一个近似值,比生产代码更像是“伪代码”。 Anyways feel free to modify as you need. 无论如何,都可以根据需要进行修改。

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(); m.init();

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

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