简体   繁体   English

jQuery向左/向右拖动

[英]jQuery drag left/right

I've just created a custom carousel with images and have got previous/next arrows to move the images around. 我刚刚用图像创建了一个自定义轮播,并使用了上一个/下一个箭头来移动图像。

Does jQuery have an event where I can click on the photo, drag it to the left or right and fire the same action that I currently have on the arrows? jQuery是否有一个事件,我可以单击照片,将其向左或向右拖动,并执行与当前箭头相同的操作?

My current code looks like this 我当前的代码如下所示

$carousel.animate({
    left: '+=' + amountToAnimate
}, 800, 'backEaseOut');

I also need to prevent Firefox from 'picking' the image up. 我还需要防止Firefox“拾取”图像。

I'm already using jQuery UI if that helps. 如果有帮助,我已经在使用jQuery UI。

You will need to add draggable() to your item and add some custom code the start event. 您将需要将draggable()添加到您的商品,并在开始事件中添加一些自定义代码。 With more sample code it might be easier to give fuller advice, jsfiddle.net sample is best 有了更多示例代码,可能更容易给出更全面的建议,jsfiddle.net示例是最佳的

EDIT: 编辑:

You could use events api http://api.jquery.com/category/events/ , mousemove and mousedown, to figure which way to move the image, and call the animate event. 您可以使用事件api http://api.jquery.com/category/events/,mousemove和mousedown来确定移动图像的方式,并调用动画事件。

Re-using draggable , with some clever options and event functions may be better 重复使用Draggable,并提供一些巧妙的选项和事件功能可能会更好

I also need to prevent Firefox from 'picking' the image up.

Use the Mozilla CSS Extensions 使用Mozilla CSS扩展

-moz-user-focus:ignore; 
-moz-user-select:none; 

might to do the trick. 可能会解决问题。

This may not be the most elegant solution but I believe it should do the job 这可能不是最优雅的解决方案,但我认为它应该能胜任

// shortcut var j = jQuery; //快捷方式var j = jQuery;

// add mouse down listener to the image j('#image_id').mousedown(function(){ //将鼠标按下侦听器添加到图像j('#image_id')。mousedown(function(){

// add mouse move
j('#image_id').mouseMove(function(event){

      // declare vars
      var previous_x_position;
      var previous_y_position;

      if(x_position)
      {
         previous_x_position = x_position;
         previous_y_position = y_position;
      }

      var x_position = event.pageX;
      var y_position = event.pageY;

      if(previous_x_position < x_position)
      {
          // we are moving right
      }
      else
      {
          // we are moving left
      }
      if(previous_y_position < y_position)
      {
          // we are moving down
      }
      else
      {
          // we are moving up
      }


})

}) })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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