Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2008
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    jQuery Draggable Array

    I've got a lot of draggable div id's that I'd like to store in a variable so that i can add divs effortlessly later, but i'm stuck on syntax (been going from Prototype to Scriptaculous to MooTools blah blah). Here's my non-working code. Sorry, I'm a visual learner.

    Code:
    <script type="text/javascript">
    $(document).ready(function(){
    						   
      var windows = [ "#sword", "#hat" ];
    						   
      windows.draggable({				
    	   start: function() {
    			$(this).fadeTo(100, 0.5);
    	   },
    	   stop: function(){
    			$(this).fadeTo(100, 1);
    	   },
    						 });
      
    });
    </script>

  • #2
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    Well jQuery uses CSS selectors, and to select multiple elements in CSS you seperate it by a comma So this is easy done via.
    Code:
    <script type="text/javascript">
    $(document).ready(function(){
    
      $('#sword, #hat').draggable({				
    	   start: function() {
    			$(this).fadeTo(100, 0.5);
    	   },
    	   stop: function(){
    			$(this).fadeTo(100, 1);
    	   },
      });
    });
    </script>

  • #3
    New Coder
    Join Date
    Nov 2008
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts
    ok awesome, thanks Iszak, that worked (only in the head of my index; wouldn't work when i tried to link to it from external javascript file?). But lets assume I've got 30 of these suckers that I want to drag around, is this still the most efficient way to accomplish this?

    Also, I'd love to be able to implement the stack option with this set of elements, but once again, I don't really know how to go about writing this. Thanks for your reply, I really really appreciate it!

  • #4
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    Erm if you're using an external javascript file place it at the bottom if there's no onload event listener (and I recommend this method). I also suggest you use a class instead of using id's so you can simply use a single selector vs. 30. As for the stack option, I'm not sure I've never used it sorry I couldn't help any more.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •