jquery拖动对象快速问题

发布于 2024-09-07 05:15:10 字数 1842 浏览 6 评论 0原文

如果我非常慢地拖动一个对象,它就会完全按照我想要的方式执行。但是,如果我做得更快一点或非常快,它就不会按预期工作。您可以看到两种结果:缓慢拖动时的预期结果和快速拖动时的完美结果。我只是一个 jquery 初学者,如果您看到 JS 代码的任何其他愚蠢部分,请告诉我。

背景中的移动对象(在拖动可拖动对象时)应每离开地图支架 20 个像素就返回到其原始位置。当快速移动时,它不会这样做,但是,当缓慢拖动扳机时,它会这样做。

现场示例: http://jsbin.com/egeki3

ps 仅拖动顶部/底部方向 仅

JS 部分(使用 jQuery UI)

    $().ready(function(){
 // map measurments
 var mapWidth = parseInt($('#map').width());
 var mapHeight = parseInt($('#map').height());

 var fragmentH = 20;
 var fragmentW = 20;

 // number of map fragments to use
 var mapBlocksH = Math.ceil(mapHeight/fragmentH)+2;
 var mapBlocksW = Math.ceil(mapWidth/fragmentW)+2;

 // the area size of map fragments displacement
 var mapH  = mapBlocksH*fragmentH;
 var mapW  = mapBlocksW*fragmentW;

 // calculate the maps fragments wrapper position, dimension
 $('#map div.map, div.drag').css({height: mapH, width: mapW, left: (mapWidth-mapW)/2, top: (mapHeight-mapH)/2});

 for(i = 0; i < mapBlocksH*mapBlocksW; i++)
 {
  $('#map div.map').append('<div></div>');
 }

 $('#map div.drag').draggable({
  revert: true,
  revertDuration: 0,
  addClasses: false,
  start : function(event, ui){
   mapOriginalOffset = $('#map div.map').offset();
   //mapOriginalOffset2 = $('#map div.map').offset();
  },
  drag : function(event, ui){
   $('#map div.map').offset({top: mapOriginalOffset.top + (ui.originalPosition.top-ui.position.top)});

   if(Math.abs($('#map div.map').offset().top-$('#map').offset().top) % fragmentH == 0)
   {
    $('#map div.map').offset({top: mapOriginalOffset.top});

    return false;
    //$('#map div.map').offset({top: mapOriginalOffset.top});

    //mapOriginalOffset2.top = $('#map div.drag').offset().top;
   }
  },
  stop : function(event, ui){
  }
 });

If I drag an object very slow, it does exactly what I want it to do. However, if I do it a bit faster or very fast, it does not work as expected. You can see both results: the expected, while dragging slow and the flawless, when dragging fast. I am just a jquery beginner, if you see any other stupid parts of the JS code, please let me know.

The moving object in the background (while dragging the draggable object) should return to its native position each 20 pixels it is moved away from map holder. It does not do that when moved fast, it does, however, when draggin the trigger slow.

The live example: http://jsbin.com/egeki3

p.s. drag top/bottom directions only

The JS part only (using jQuery UI)

    $().ready(function(){
 // map measurments
 var mapWidth = parseInt($('#map').width());
 var mapHeight = parseInt($('#map').height());

 var fragmentH = 20;
 var fragmentW = 20;

 // number of map fragments to use
 var mapBlocksH = Math.ceil(mapHeight/fragmentH)+2;
 var mapBlocksW = Math.ceil(mapWidth/fragmentW)+2;

 // the area size of map fragments displacement
 var mapH  = mapBlocksH*fragmentH;
 var mapW  = mapBlocksW*fragmentW;

 // calculate the maps fragments wrapper position, dimension
 $('#map div.map, div.drag').css({height: mapH, width: mapW, left: (mapWidth-mapW)/2, top: (mapHeight-mapH)/2});

 for(i = 0; i < mapBlocksH*mapBlocksW; i++)
 {
  $('#map div.map').append('<div></div>');
 }

 $('#map div.drag').draggable({
  revert: true,
  revertDuration: 0,
  addClasses: false,
  start : function(event, ui){
   mapOriginalOffset = $('#map div.map').offset();
   //mapOriginalOffset2 = $('#map div.map').offset();
  },
  drag : function(event, ui){
   $('#map div.map').offset({top: mapOriginalOffset.top + (ui.originalPosition.top-ui.position.top)});

   if(Math.abs($('#map div.map').offset().top-$('#map').offset().top) % fragmentH == 0)
   {
    $('#map div.map').offset({top: mapOriginalOffset.top});

    return false;
    //$('#map div.map').offset({top: mapOriginalOffset.top});

    //mapOriginalOffset2.top = $('#map div.drag').offset().top;
   }
  },
  stop : function(event, ui){
  }
 });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无名指的心愿 2024-09-14 05:15:10

当您拖动对象时,该对象不会占据所有可能的位置,而是会跳过一些像素。

我记录了 .top 位置,其值为 8、11、18、27,因此条件为

Math.abs($('#map div.map').offset().top-$('#map').offset().top) % fragmentH == 0

true 的情况很少见。

对于您的情况,最好有类似的东西

Math.abs($('#map div.map').offset().top-$('#map').offset().top) >= fragmentH

它仅取决于您确切想要做什么,但不要使用像素算术和模数拖着走,坏主意!

When you drag an object, the object doesn't take all the position possible, but skip some pixel.

I log the .top position, and the value are 8, 11, 18, 27, so it is rare that the condition

Math.abs($('#map div.map').offset().top-$('#map').offset().top) % fragmentH == 0

is true.

For your case, it is better to have something like

Math.abs($('#map div.map').offset().top-$('#map').offset().top) >= fragmentH

It only depends on what you exactly want to do, but do not use modulo with pixel arithmetic & dragging, bad idea !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文