Solved

jQuery container slider

Posted on 2011-03-23
2
429 Views
Last Modified: 2012-05-11
Hi Guys,

I am wanting to slide the internal contents horizontally of a div via a href links.
I have coded the beginning of what I want to do but can't find the exact answer on the web to complete.

Looking at the code:
If i was to click on 'Slide to Area B' i would like the div 'inner-container' to slide to the left and to have the 2nd area (areaB) to align up with the beginning of 'inner-container'.  And when i click on 'Slide to Area A'  for it to slide back.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	overflow-x: hidden;
}
#container {
	position:absolute;
	top: 100px;
	left: 100px;
}
#inner-container {
	position:relative;
	width: 4000px;
	height: 300px;
	background-color:#CCCCCC;
}
#areaA {
	position:absolute;
	top: 0;
	left: 0;
	width: 300px;
	height: 300px;
	background-color:#0033FF;
} 
#areaB {
	position:absolute;
	top: 0;
	left: 300px;
	width: 300px;
	height: 300px;
	background-color:#FF0000;
} 
#nav {
	position:absolute;
	top: 430px;
	left: 100px;
}
</style>
</head>
<body>

<div id="container">
	<div id="inner-container">
  	<div id="areaA">Content A</div>
    <div id="areaB">Content B</div>
  </div>
</div>

<div id="nav">
	<div><a href="#">Slide to Area A</a></div>
	<div><a href="#">Slide to Area B</a></div>
</div>

</body>
</html>

Open in new window


Also if i was to load the page with #areaA at the end of the URL for it to show that part of the inner-container DIV.

I'm sure jQuery can do it, or some sort of Javascript.

Hope someone can show me an answer.
Thanks
0
Comment
Question by:dev09
2 Comments
 
LVL 13

Accepted Solution

by:
darren-w- earned 250 total points
ID: 35205524
From reading your description, I'm guessing this?:
<html>
    <head>
        <title>test</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<style>
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	overflow-x: hidden;
}
#container {
	position:absolute;
	top: 100px;
	left: 100px;
}
#inner-container {
	position:relative;
	width: 4000px;
	height: 300px;
	background-color:#CCCCCC;
}
#areaA {
	position:absolute;
	top: 0;
	left: 0;
	width: 300px;
	height: 300px;
	background-color:#0033FF;
}
#areaB {
	position:absolute;
	top: 0;
	left: 300px;
	width: 300px;
	height: 300px;
	background-color:#FF0000;
}
#nav {
	position:absolute;
	top: 430px;
	left: 100px;
}
</style>
        <script type="text/javascript">
          $(function(){
              $("#s2b").click(function(){
                  $("#container").animate({
                      left:'-=75'
                  },2500,function(){})
                  
                     $("#areaB").animate({
                      left:'-=300'
                  },2500,function(){})

              })
              $("#s2a").click(function(){
                  $("#container").animate({
                      left:'+=75'
                  },2500,function(){})

                     $("#areaB").animate({
                      left:'+=300'
                  },2500,function(){})

              })})
         
        </script>
    </head>
    <body>
        <div id="container">
            <div id="inner-container">
                <div id="areaA">Content A</div>
                <div id="areaB">Content B</div>
            </div>
        </div>

        <div id="nav">
            <div><a href="#" id="s2a">Slide to Area A</a></div>
            <div><a href="#" id="s2b">Slide to Area B</a></div>
        </div>
    </body>
</html>

Open in new window

0
 
LVL 12

Assisted Solution

by:Amick
Amick earned 250 total points
ID: 35205529
Here are two methods that might get you close, but need development to fit your needs.
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	overflow-x: hidden;
}
#container {
	position:absolute;
	top: 100px;
	left: 100px;
}
#inner-container {
	position:relative;
	width: 4000px;
	height: 300px;
	background-color:#CCCCCC;
}
#areaA {
	position:absolute;
	top: 0;
	left: 0;
	width: 300px;
	height: 300px;
	background-color:#0033FF;
}
#areaB {
	position:absolute;
	top: 0;
	left: 300px;
	width: 300px;
	height: 300px;
	background-color:#FF0000;

}
#nav {
	position:absolute;
	top: 430px;
	left: 100px;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
Aclicked=0;
Bclicked=0;
$(document).ready(function(){
  $("button#ButtonA").click(function(){
	if (Aclicked==0){
    	$("div#areaA").animate({left:"300px"},"slow");
    	$("div#areaB").fadeTo("slow",0);

    	Aclicked=1;
    } else {
    	$("div#areaA").animate({left:"0"},"slow");
     	$("div#areaB").fadeTo("slow",1);

    	Aclicked=0;
	}

  });
  $("button#ButtonB").click(function(){
	if (Bclicked==0){
    	$("div#inner-container").animate({left:"300px"},"slow");

    	$("div#areaB").animate({left:"0"},"slow");
    	Bclicked=1;
    } else {
    	$("div#inner-container").animate({left:"0"},"slow");
    	$("div#areaB").animate({left:"300px"},"slow");

    	Bclicked=0;
	}

  });


});

</script>

</head>
<body>

<div id="container">
	<div id="inner-container">
  	<div id="areaA">Content A</div>
    <div id="areaB">Content B</div>
  </div>
</div>

<div id="nav">
	<div><a href="#ButtonA">Slide to Area A</a></div>
	<div><a href="#ButtonB">Slide to Area B</a></div>
</div>

<button id="ButtonA">Method A</button>
<button id="ButtonB">Method B</button>
<br /><br />


</body>
</html>

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ROBOFTP UNZIP 1 46
add a 3rd selection to sum 7 29
Mobile page Scrolling down when refreshing 4 33
Checkout Page Input Field not aligned 1 23
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question