简体   繁体   中英

How to make an image (div) follow the cursor within a website?

This image is a static div duplicate of the cursor trailimage. One is fixed, the other follows the cursor. How would I code the static div image to follow the cursor?

refer to this image http://deverexdrawer.deviantart.com/art/Trailimage-538913342

Trailimage javascript excerpts

var trailimage=["images/contact/gardening-glove-cursor.png", , ]
var offsetfrommouse=[-110,5]
var displayduration=0

if (document.getElementById || document.all)
    document.write('<div id="trailimageid" style="position:absolute;visibility:visible;left:0px;top:100px;width:1px;height:1px"><img border="0" src="'+trailimage[0]+'"></div>')

function followmouse(e){
    var xcoord=offsetfrommouse[0]
    var ycoord=offsetfrommouse[1]
    if (typeof e != "undefined"){
        xcoord+=e.pageX
        ycoord+=e.pageY
}

</script>

Static div image

...<img border="0" src="'+trailimage[0]+'"
onmousedown="this.src=trailimage[1]"   
onmouseup="this.src=trailimage[0]" />...

assuming

var trailimage=[
  "images/contact/gardening-glove-cursor.png",
  "images/contact/gardening-glove-cursor_down.png" ]

Try this

http://jsfiddle.net/n4foofvm/1/

html

<div id="movingDiv">
    <img id="barber" class="barber-image" src="1.jpg" />
</div>

css

#movingDiv{
    width:20px;
    height:20px;
    position:absolute;
}

javascript

$(document).mousemove(function(e){
    $("#movingDiv").css({left:e.pageX, top:e.pageY});
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM