Results 1 to 2 of 2

Thread: jQuery/Javascript Swap Position of 2 Divs Completely & FadeIn/Out on Toggle Button.

  1. #1
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default jQuery/Javascript Swap Position of 2 Divs Completely & FadeIn/Out on Toggle Button.

    Hi,

    I'm fairly new to coding and have come across a few problems.

    I'm trying to swap two Div's including their content on the click of a button preferably using a toggle so I can swap and then swap back on an alternate click.

    Button=Swap
    Div=News
    Div=Article

    (Just so to make it easier to understand)

    Secondly, I'm also trying to have an image fading in and out on a toggle button too. I can currently get the image to fade in and out on a single click but cannot get it to work on alternate clicks
    i.e.
    Click 1 - Fade In
    Click 2 - Fade Out

    Appreciate any help.

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    I think this does what you want with the div switch;

    Code:
    <html>
    <head>
    <title>Div Swop</title>
    
    <style type="text/css">
    p { line-height:200px; text-align:center }
    #div1 { background:yellow; width:200px; height:200px; }
    #div2 { background:pink; width:200px; height:200px; }
    </style>
    
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">
    	$(document).ready(function()
    		{
                   	$('#button').toggle(
                        function()
    			{
                            $('#div1').css({'display':'none'});
                            $('#div2').slideDown(); // or try "fadeIn()"
                            $('#button-img').attr('src','images/button-clicked.jpg');
                        	}, 
    			function() 
    			{
                            $('#div2').css({'display':'none'});
                            $('#div1').slideDown(); // or try "fadeIn()"
                            $('#button-img').attr('src','images/button.jpg');
                        	});
              	});
    </script>
    
    </head>
    <body>
    
    
    <div id="button">
    	<img src="images/button.jpg" id="button-img"/>
    </div>
    
    <div id="div1">
    	<p>...content in Div One ...</p>
    </div>
    
    <div id="div2" style="display:none;">
    	<p>...switch to Div Two ...</p>
    </div>
    
    
    </body>
    </html>
    Not sure what you mean with the image thing though - if its to swop the button image, then thats covered in my code example above.
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

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
  •