在另一个div内的mousemove上移动div [英] moving a div on mousemove inside another div

查看:221
本文介绍了在另一个div内的mousemove上移动div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使容器div内的div在X轴上沿鼠标的相反方向移动(仅向左和向右).

I am trying to make a div which is inside a container div move in the opposite direction of the mouse on the X axis (only left and right).

这是我到目前为止所拥有的:

this is what I have so far:

html

<div class="container">
    <div class="box"></div>
</div>

css

.container { width: 450px; height: 52px; border: 1px #000 solid; position: relative; }
.box { width: 50px; height: 50px; border: 1px #000 solid; position: absolute; right: 200px; }

jquery

$(document).ready(function(){
  $('div.container').mousemove(function(e){
        var x = e.pageX - this.offsetLeft;
      if ($('div.box').css('right') <= '400') {
          $('div.box').css({'right': x}); 
      }
  });
});

JSfiddle- http://jsfiddle.net/eyalbin/qQmu7/49/

JSfiddle - http://jsfiddle.net/eyalbin/qQmu7/49/

由于某种原因,该功能会在几秒钟后停止工作.

for some reason the function stops working after a couple of seconds.

任何人都可以帮忙

推荐答案

尝试一下: JSFiddle

您写了

if ($('div.box').css('right') <= '400')

代替

if (x <= 400)

:)

这篇关于在另一个div内的mousemove上移动div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆