javascript - 使用 jQuery 将光标更改为自定义图像

标签 javascript jquery plugins cursor

我正在尝试编写 jQuery 插件,将默认光标替换为必要的图像,但我遇到了 mouseleave 问题。它不会触发,因为光标始终位于子 div 之前并且永远不会离开它,因此光标永远不会变回默认值。

DEMO

有什么解决办法吗?

顺便说一句:jQuery mouseleave 行为有点奇怪。仅当您离开您的 div 和所有子级 div 时它才会触发。

DEMO 2

我的代码:

// Plugin changing cursor before element
;
(function ($) {
    $.fn.changeCursor = function (cursorPicUrl, dx, dy) {
        function inFunction(e) {
            $cursor.show();
            return false;
        }
        function outFunction(e) {
            $cursor.hide();
            return false;
        }
        function moveFunction(e) {
            var x = e.clientX-100;
            var y = e.clientY-100;
            $cursor.css({
                "transform": "translate(" + x + "px," + y + "px)"
            });
        }
        var $cursor = $('<div id="#custom-cursor"></div>').css({
            /*cursor: none;*/
            width:      '150px',
            height:     '150px',
            background: 'url("' + cursorPicUrl + '") no-repeat left top',
            position:   'absolute',
            display:    'none',
            'z-index':  '10000'
        });

        this.append( $cursor )
            .on( "mouseenter", inFunction )
            .on( "mouseleave", outFunction )
            //.hover( inFunction, outFunction)
            .mousemove( moveFunction );
        return this;
    };

})(jQuery);

$(document).ready(function () {
    var url = 'http://placehold.it/150x150';
    $('#test-area').changeCursor( url );

});

更新

我的孤独:

<强> jquery.change-cursor

最佳答案

我做了一些调整:

  1. this存储在前面的变量中。
  2. 将光标 div 附加到正文。
  3. 将顶部/左侧属性添加到光标。

DEMO

Javascript:

(function ($) {
    $.fn.changeCursor = function (cursorPicUrl, dx, dy) {
        var elem = this;
        function inFunction(e) {
            $cursor.show();
            return false;
        }
        function outFunction(e) {
            $cursor.hide();
            return false;
        }
        function moveFunction(e) {
            var x = e.clientX;
            var y = e.clientY;
            $cursor.css({
                "transform": "translate(" + x + "px," + y + "px)"
            });
        }
        var $cursor = $('<div id="#custom-cursor"></div>').css({
            /*cursor: none;*/
            width:      '150px',
            height:     '150px',
            background: 'url("' + cursorPicUrl + '") no-repeat left top',
            position:   'absolute',
            top: '0',
            left: '0',
            display: 'none'
        });

        $('body').append( $cursor );
        elem.on( "mouseenter", inFunction )
          .on( "mouseleave", outFunction )
          .mousemove( moveFunction );
        return this;
    };

})(jQuery);

关于javascript - 使用 jQuery 将光标更改为自定义图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26490698/

相关文章:

scala - Sbt-assembly Unresolved 依赖项,Scala

javascript - 我想使用模式通过 php 文件发送邮件,并且我希望在提交关闭后关闭弹出窗口

jquery - 使用 HTML5 或 jQuery 增加项目符号?

javascript - 如何让分页路由在 Ember.js 中正确加载?

javascript - 当我的增量游戏代码运行时,html 文件中的按钮实例未加载

javascript - Jquery 在 HTML 中的两个特定单词之间切换内容

version-control - Maven SCM插件-什么是url配置值?

php - 从 WP Job Manager 列表中删除永久链接的公园

javascript - 使用 Three.js 从 DDS 立方体贴图文件加载立方体贴图

javascript - 使用按钮滚动 slider