How to write a jQuery animate in this situation?

Discussion in 'JavaScript' started by youlichika, Nov 18, 2010.

  1. #1
    I need a jqeury animate effection. when I click img1.jpg, img2.jpg will animate right 200px, but I have too many divs and other html elements, I tried many times, it also failed. How to write correctly? Thanks. PS: h1, the font is on the top of image, and b class="effect" is some fadein effect, it should be the toppest of the all.

    <script type="text/javascript">  
    jQuery(".font1").click(function(){ 
      jQuery(".div21").animate({"right": "+=200px"}, "slow"); 
    }); 
    </script> 
    <a href="#" class="click"> 
    <div class="div11" style="float:left;"> 
    <h1 class="font1" style="z-index:5;">text1</h1> 
    <div class="div12"> 
    <div class="div13">  
    <img src="img1.jpg"> 
    <b class="effect" style="z-index:10;"></b> 
    </div> 
    </div> 
    </div> 
    </a> 
    <a href="#" class="click"> 
    <div class="div21" style="float:right;"> 
    <h1 class="font2" style="z-index:5;">text2</h1> 
    <div class="div22"> 
    <div class="div23"> 
    <img src="img2.jpg"> 
    <b class="effect" style="z-index:10;"></b> 
    </div> 
    </div> 
    </div> 
    
    HTML:

     
    youlichika, Nov 18, 2010 IP
  2. GNi33

    GNi33 Peon

    Messages:
    16
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    so what is it doing right now as it is? nothing?
     
    GNi33, Nov 19, 2010 IP
  3. youlichika

    youlichika Greenhorn

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    Yes, nothing happened.
     
    youlichika, Nov 19, 2010 IP
  4. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Have to use marginRight because right only works if the element has absolute position.
    
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
    <script type="text/javascript">  
    jQuery(document).ready(function(){
       jQuery(".font1").click(function(){ 
          jQuery(".div21").animate({"marginRight": "+=200px"}, "slow"); 
       }); 
    });
    </script> 
    
    Code (markup):
    If you want to use right, you need to make this change too:
    <div class="div21" style="float:right; position:absolute; right:0;">

    Your code only works when the heading is clicked. If you want it to happen when the image is clicked, add the class to the image as well.
    <img class="font1" src="img1.jpg">
     
    Last edited: Nov 21, 2010
    Cash Nebula, Nov 21, 2010 IP
  5. youlichika

    youlichika Greenhorn

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5
    Well, this can solve my problem. Thanks friend.
     
    youlichika, Nov 22, 2010 IP