Make Background image move with mouse in Jquery

I found this [snippet of code][1], and tweaked it a bit for my website: var vH=jQuery('.categoryWrapper').height(); var vW=jQuery('.categoryWrapper').width(); var vT=jQuery('.categoryWrapper').offset().top; var vL=jQuery('.categoryWrapper').offset().left; jQuery('.categoryWrapper').mousemove(function(e){ var ypos=e.pageY-vT; var xpos=e.pageX-vL; var y=Math.round(ypos/vW*100); var x=Math.round(xpos/vH*100); jQuery(this).css({backgroundPosition: x+'% '+y+'%'}); However, it's not quite doing what I want it to, and I am having a hard time tweaking this to work right. Go to http://photoshop.tutorialcraft.com , and move your mouse over the "Photoshop Tutorials, "Graphic Inspiration", and "Graphic Downloads" buttons. Basically, I have a PNG file with a radial gradient. I want the background to track the mouse, but move a little faster than the mouse. For instance, `.categoryWrapper` is 312 x 88 pixels. The actually background img is 277x277. If I move my mouse in via the top of `.categoryWrapper`, the center of the bg IMG should be about 200px above the point where the mouse entered. As you move the mouse to the bottom, the background should move to about 200px below the mouse (that way the bg img appears and disappears when you move in and out). Below image should illustrate what I am trying to do: ![enter image description here][2] [1]: http://webmastereseller.com/background_mover/ [2]: http://i.stack.imgur.com/o9a2v.jpg

以上就是Make Background image move with mouse in Jquery的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏