旋转画布仅在第一次有效

发布于 2025-01-04 07:03:12 字数 1203 浏览 2 评论 0原文

我试图在每次鼠标悬停时旋转画布,但它只是第一次工作。
你可以在这里玩一下: http://jsfiddle.net/h3Z7j/2/

我的原因将其旋转 0 度是为了在加载时将其转换为画布,因为我是第一次加载 img

旋转脚本:http:// code.google.com/p/jquery-rotate/downloads/detail?name=jquery.rotate.1-1.js

<div class="gameboard">
    <img width="40" height="40" id="1-1" src="http://www.netbsd.org/images/download-icon-orange.png" alt="">
    <img width="40" height="40" id="2-1" src="http://www.netbsd.org/images/download-icon-orange.png" alt="">
    <img width="40" height="40" id="1-2" src="http://www.netbsd.org/images/download-icon-orange.png" alt="">
    <img width="40" height="40" id="2-2" src="http://www.netbsd.org/images/download-icon-orange.png" alt="">
</div>

$(document).ready(function () {
    $(".gameboard img").each(function () {
        $(this).rotateLeft(0);
    });
    $(".gameboard canvas").mouseover(function () {
        $(this).rotateLeft();
    });
});  

编辑
如果有其他更好的轮换脚本,我会很乐意更改为该脚本。

I'm trying to rotate a canvas on every mouseover, but it's only working the first time.
You can play with it here: http://jsfiddle.net/h3Z7j/2/

The reason I'm rotating it 0 degrees is to convert it to canvases on load, since I'm loading img's the first time.

rotation script: http://code.google.com/p/jquery-rotate/downloads/detail?name=jquery.rotate.1-1.js

<div class="gameboard">
    <img width="40" height="40" id="1-1" src="http://www.netbsd.org/images/download-icon-orange.png" alt="">
    <img width="40" height="40" id="2-1" src="http://www.netbsd.org/images/download-icon-orange.png" alt="">
    <img width="40" height="40" id="1-2" src="http://www.netbsd.org/images/download-icon-orange.png" alt="">
    <img width="40" height="40" id="2-2" src="http://www.netbsd.org/images/download-icon-orange.png" alt="">
</div>

$(document).ready(function () {
    $(".gameboard img").each(function () {
        $(this).rotateLeft(0);
    });
    $(".gameboard canvas").mouseover(function () {
        $(this).rotateLeft();
    });
});  

edit
If there are other rotation scripts that work better I'll happily change to that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赢得她心 2025-01-11 07:03:12

基于 Rory 的答案,您可以通过在 mouseover/mouseout 上将标志设置为 true/false 来实现每次鼠标悬停旋转一圈:

$(document).ready(function () {
    var stopSpin = false;
    $(".gameboard img").each(function () {
        $(this).rotateLeft(0);
    });
    $(".gameboard").delegate('canvas', 'mouseover', function () {
        if (!stopSpin){
            $(this).rotateLeft();
        }
        stopSpin = true;
    });
    $(".gameboard").delegate('canvas', 'mouseout', function () {
        stopSpin = false;
    });
});

请参阅: http: //jsfiddle.net/CK5hh/

Building on Rory's answer you can achieve one spin per mouseover by setting a flag to true/false on mouseover/mouseout:

$(document).ready(function () {
    var stopSpin = false;
    $(".gameboard img").each(function () {
        $(this).rotateLeft(0);
    });
    $(".gameboard").delegate('canvas', 'mouseover', function () {
        if (!stopSpin){
            $(this).rotateLeft();
        }
        stopSpin = true;
    });
    $(".gameboard").delegate('canvas', 'mouseout', function () {
        stopSpin = false;
    });
});

See: http://jsfiddle.net/CK5hh/

浅笑轻吟梦一曲 2025-01-11 07:03:12

问题是因为 canvas 元素被删除,然后读取到 DOM。然后,这将丢失放置在其上的 mouseover 处理程序。如果您使用 delegate 附加事件,它会起作用:

$(document).ready(function () {
    $(".gameboard img").each(function () {
        $(this).rotateLeft(0);
    });
    $(".gameboard").delegate('canvas', 'mouseover', function () {
        $(this).rotateLeft();
    });
});

虽然我不确定效果是否正是您想要的:)

小提琴示例

The problem is because the canvas element is being removed, and then readded to the DOM. This is then losing the mouseover handler placed on it. If you attach your event with delegate instead, it works:

$(document).ready(function () {
    $(".gameboard img").each(function () {
        $(this).rotateLeft(0);
    });
    $(".gameboard").delegate('canvas', 'mouseover', function () {
        $(this).rotateLeft();
    });
});

Although I'm not sure the effect is exactly what you're after :)

Example fiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文