0

我编写了这个脚本来在网页中制作一个可转动的按钮。它工作得很好,但它不想退出该功能。它继续运行括号内的代码。知道我该如何解决吗?这是我现在拥有的代码。

如果您按下鼠标并拖动,则该值将发生变化。但现在它无处不在,只有当你点击按钮而不是拖动时才会发生这种情况。

function pressed(){
var PosibleVal = new Array(8,41,74,107,140,174);
$(window).on("mousemove", function(e) {
    if (e.which == 1) { // if left mouse button is pressed
      var posY=(e.pageY - 158)*(-1); // read the mouse Y-position 

        if(posY<0)
        {
            posY=0;
        }
        if(posY>180)
        {
            posY=180;
        }
        var ar = Math.round(posY/33); //Round number 

        var cssPosY = "-webkit-transform:rotate("+PosibleVal[ar]+"deg)";
        $("#innerheater").attr("style", cssPosY); // change rotation
    } 
});

} 

这里是示例的链接。(它仅适用于 chrome 和 safari)

4

1 回答 1

0

我会这样做

<script type="text/javascript">
$(document).ready(function()
{
    var PosibleVal = new Array(8,41,74,107,140,174);
    $('#heater').on("mousemove", function(e)
    {
         if (e.which == 1) {
         var posY=(e.pageY - 158)*(-1);

         if(posY<0)
            posY=0;
         if(posY>180)
            posY=180;
         var ar = Math.round(posY/33); //geen komma getal
         var cssPosY = "-webkit-transform:rotate("+PosibleVal[ar]+"deg)";
         $("#innerheater").attr("style", cssPosY);
    } 
});
</script>

在#heater div 上设置onMouseMove 事件对我来说更合乎逻辑。然后,您可以将其替换<div id="heater" onclick="pressed()"><div id="heater">.

于 2013-03-23T12:15:29.100 回答