Change the mouse pointer using JavaScript

后端 未结 4 1451
别那么骄傲
别那么骄傲 2020-12-05 06:59

I wanted to use a script to change the mouse pointer on my website using JavaScript. It\'s better done by CSS but my requirement is of a script that can be distributed to ma

4条回答
  •  一整个雨季
    2020-12-05 07:24

    Javascript is pretty good at manipulating css.

     document.body.style.cursor = *cursor-url*;
     //OR
     var elementToChange = document.getElementsByTagName("body")[0];
     elementToChange.style.cursor = "url('cursor url with protocol'), auto";
    

    or with jquery:

    $("html").css("cursor: url('cursor url with protocol'), auto");
    

    Firefox will not work unless you specify a default cursor after the imaged one!

    other cursor keywords

    Also remember that IE6 only supports .cur and .ani cursors.

    If cursor doesn't change: In case you are moving the element under the cursor relative to the cursor position (e.g. element dragging) you have to force a redraw on the element:

    // in plain js
    document.getElementById('parentOfElementToBeRedrawn').style.display = 'none';
    document.getElementById('parentOfElementToBeRedrawn').style.display = 'block';
    // in jquery
    $('#parentOfElementToBeRedrawn').hide().show(0);
    

    working sample:
    
    
    
    
    First jQuery-Enabled Page
    
    
    
    
    hello with a fancy cursor!

提交回复
热议问题