简体   繁体   中英

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?

My current code looks like this

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

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

I'm already using jQuery UI if that helps.

You will need to add draggable() to your item and add some custom code the start event. With more sample code it might be easier to give fuller advice, jsfiddle.net sample is best

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.

Re-using draggable , with some clever options and event functions may be better

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

Use the Mozilla CSS Extensions

-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;

// add mouse down listener to the image 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
      }


})

})

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