jQuery 在嵌套函数调用中更改 CSS

标签 jquery css nested-function

我目前正在尝试创建一个拖放元素,它允许我移动它所附加的元素。一切都是动态创建的。我遇到了一个重复出现的问题,我无法从嵌套函数调用内部更改 css。

jQuery(不工作)

//Drag begin     
var dragX;
var dragY;
var mouseDown = false;
var dragIcon = $('<span>')
.css({
    'z-index' : '11',
    'position' : 'absolute',
    'width' : '300px',
    'height' : '140px',
    'left' : '0px',
    'top' : '20px'
})

.addClass('unselectable')

.mousedown(function(){
    mouseDown = true;
    $(this).mousemove(function(){
        if(mouseDown == true){
            var dragX = getCurrentMouseX();
            var dragY = getCurrentMouseY();

            //Don't allow ideas to be created too close to the border of the page
            if(dragX < 260){
                dragX = 260;
            }else if(dragX > window.innerWidth - 260){
                dragX = window.innerWidth - 260;
            }    

            if(dragY < 160){
                dragY = 160;
            } else if(dragY > window.innerHeight - 450){
                dragY = window.innerHeight - 450;
            }

            $(this).css({
                'left' : dragX - 150,
                'top' : dragY - 81
            });
        }                
    })
})

.mouseout(function(){
    mouseDown = false;
})
.mouseup(function(){
     mouseDown = false;
});  

我已经用 JavaScript 编写了可以正常工作的函数,但是我正在尝试将其转换为 jQuery。以下是有效的 JavaScript 函数:

JavaScript(工作)

        //Drag begin     
    var dragX;
    var dragY;
    var mouseDown = false;

    //TODO: An icon which can be used to drag the cloud around
    var dragIcon = document.createElement('span');
    dragIcon.style.zIndex = "11";
    dragIcon.style.position = "absolute";
    dragIcon.style.width = "300px";
    dragIcon.style.height = "140px";   
    dragIcon.setAttribute('class', 'unselectable');
    dragIcon.style.left = "0px";
    dragIcon.style.top = "20px";
    dragIcon.unselectable = "on";
    dragIcon.style.MozUserSelect = "none";

    dragIcon.onselectstart = function() {return false;}
    dragIcon.onmousedown = function(){
        mouseDown = true;
        dragIcon.onmousemove = function(){
            if(mouseDown == true){
                var dragX = getCurrentMouseX();
                var dragY = getCurrentMouseY();

                if(dragX < 260){
                    dragX = 260;
                }else if(dragX > window.innerWidth - 260){
                    dragX = window.innerWidth - 260;
                }    

                if(dragY < 160){
                    dragY = 160;
                } else if(dragY > window.innerHeight - 450){
                    dragY = window.innerHeight - 450;
                }                           
                cloudStyle.left = dragX  - 150 + "px"; //-150 to make up for the position of the drag icon
                sunRay.style.left = dragX - 220 + "px";
                cloudStyle.top = dragY - 81 + "px"; //-85 to make up for the positioning
                sunRay.style.top = dragY - 250 + "px"; 
            }
        }
    }

    dragIcon.onmouseout = function(){
        mouseDown = false;
    }
    dragIcon.onmouseup = function(){
        mouseDown = false;
    }

您能否指出我哪里出错了,以及我是否采取了错误的方法。谢谢。

最佳答案

好吧,我发现要替换现有样式,我需要执行以下操作...

        item.removeAttr("style");            
        item.attr("style", "style goes here!");

删除样式属性并重新应用似乎效果很好。

关于jQuery 在嵌套函数调用中更改 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6976143/

相关文章:

javascript - 如何按照输入顺序获取关联数组的元素

javascript - 根据是否选中复选框显示隐藏内容

javascript - 如何防止在 React 中重新渲染随机颜色

css - 有没有办法用 CSS 隐藏列表中的第 n 个元素?

python - Swagger-ui 连接未找到 Python 嵌套函数

jquery - 如何使用jquery插件在播放结束时重新显示视频预览帧 "tubeplayer"

javascript - JQuery UI slider 在 0 时不起作用?

css - 无法摆脱 CSS 粘性页脚下方的边距

JavaScript - 嵌套函数删除数组的 .length

c - 函数内的函数