开发者

How to get the mouse cursor do a grab on mouse down?

开发者 https://www.devze.com 2023-04-03 20:22 出处:网络
I have made a demo on JSFIDDLE UPDATE this version works in FF but not chrome.. UPDATE 2 This website seems to have a working solution.

I have made a demo on JSFIDDLE

UPDATE this version works in FF but not chrome..

UPDATE 2 This website seems to have a working solution.

my Javascript is as follows

$("#click").live({
  mousedown: function() {
            this.addClass("closed");
        },
  mouseup: funct开发者_开发技巧ion() {
            this.removeClass("closed");
        }
});

and the CSS is as follows

.closed {
    cursor: url(https://mail.google.com/mail/images/2/closedhand.cur), default !important;
}

But why doesn't the cursor become a closed hand on mouse down with this jQuery code? Thank you so much! Any help would be appreciated!


You can get the mouse cursor grab with CSS only:

/* Standard cursors */    
.element { cursor: grab; }
.element:active { cursor: grabbing; }

/* Custom cursors */
.element { cursor: url('open-hand.png'), auto; }
.element:active { cursor: url('closed-hand.png'), auto; }


2016 Update:

Needed to do this in a jQuery slider plugin I'm working on. What I did was define the cursors in CSS, then add/remove them on touch/mouse events with jQuery.

css:

.element:hover{
    cursor: move;
    cursor: -webkit-grab;
    cursor: grab;
}
.element.grabbing { 
    cursor: grabbing; 
    cursor: -webkit-grabbing;
}

JS:

$(".element").on("mousedown touchstart", function(e) {
    $(this).addClass('grabbing')
})

$(".element").on("mouseup touchend", function(e) {
    $(this).removeClass('grabbing')
})


$(document).ready(function() {
  $(".surface").mouseup(function(){
        $(".surface").css( "cursor","crosshair");
      }).mousedown(function(){
        $(".surface").css( "cursor","wait");
      });
});

Make in css .surface{cursor:crosshair;} If you want to have mouse down/up just change the cursors to hand up/down icon. Hope this helps.


the syntax you're using wasn't introduced until 1.4.3 (documented here), your jsFiddle uses 1.4.2. Your jsFiddle also adds a class to this, rather than $(this).

But I'm also not sure about how the cursor CSS reacts to mousedown and mouseup - I have a feeling that may be limiting in some browsers, a bit of fiddling got it working on mouseup, but not mousedown - which seems strange.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号