Results 1 to 2 of 2

Thread: how to: drag LOTS of images same page?

  1. #1
    Join Date
    Oct 2007
    Location
    Near San Antonio, Texas
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up how to: drag LOTS of images same page?

    1) Script Title: DOM Drag & Drop script

    2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex11/domdrag/

    3) Describe problem: the script works fine, however, i'd like to use it on many multiples of an image (a building block). (i'd like to have an undepleatable stack of perhaps 40 or 50 blocks, all absolutely positioned one on top of the other, that can be moved over an architectural grid background to create a floor plan)

    what is the easiest way? i don't want to use a separate id for each item if i don't have to, is there a way to combine the following:

    <img id="block" src="block.gif" style="position: relative" >
    <script type="text/javascript">
    Drag.init(document.getElementById("block"));
    </script>

    <img id="block2" src="block.gif" style="position: relative" >
    <script type="text/javascript">
    Drag.init(document.getElementById("block2"));
    </script>

    <img id="block3" src="block.gif" style="position: relative" >
    <script type="text/javascript">
    Drag.init(document.getElementById("block2"));
    </script>

    etc, etc,

    also, they mention soemthing about on page load, but they don't give an example of how to use this. would the page load method work better for my application?

    any help would be GREATLY APPRECIATED!!

  2. #2
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    There are many ways to approach this, depending on whether you don't wish to give each IMG any ID attribute period, or that you simply don't want to have to manually declare them inside Drag.init() each time. Assuming the former, one way is to just assign images on the page you wish to be draggable a specific CSS class name, for example, "drag, and have the script dynamically scan and make them all draggable:

    Code:
    <img src="test.gif" class="drag" style="position: relative;" />
    <img src="test.gif" class="drag" style="position: relative;" />
    <img src="test.gif" class="drag" style="position: relative;" />
    <img src="test.gif" style="position: relative;" />
    
    <script type="text/javascript">
    
    var allimages=document.getElementsByTagName("img")
    
    for (var i=0; i<allimages.length; i++){
    	if (/drag/i.test(allimages[i].className)) //if image contains "drag" CSS class name
    		Drag.init(allimages[i])
    }
    
    </script>
    Regarding page load, it simply means wrapping the initializtion code in window.onload:

    Code:
    <script type="text/javascript">
    
    window.onload=function(){
    var allimages=document.getElementsByTagName("img")
    
    for (var i=0; i<allimages.length; i++){
    	if (/drag/i.test(allimages[i].className)) //if image contains "drag" CSS class name
    		Drag.init(allimages[i])
    }
    }
    </script>

Bookmarks

Posting Permissions

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