CSS3 Animations Slide-In Div
I'm creating an html file with the basic structure of this:
Solution 1:
Here is a jquery solution (Since you have said yes to the jquery solution in comments)
$(".next").click(function(){
var current = $(".container").css("left");
if(current == "-100px"){
current="-200px";
}
else if(current == "0px"){
current="-100px";
}
$(".container").css("left",current);
});
$(".prev").click(function(){
var current = $(".container").css("left");
if(current == "-100px"){
current="0px";
}
else if(current == "-200px"){
current="-100px";
}
$(".container").css("left",current);});
.row{
border:1px solid black;
height:100px;
margin:0;
width:100px;
padding:0;
display:block;
position:relative;
overflow:hidden;
}
.container{
height:100px;
margin:0;
width:300px;
padding:0;
display:block;
position:absolute;
left:-100px;
top:0;
-webkit-transition:left 0.5s ease-in-out;
-moz-transition: left 0.5s ease-in-out;
transition: left 0.5s ease-in-out;
}
.ins{
width:100px;
float:left;
height:100px;
margin:0;
padding:0;
background-color:red;
}
.div2{
background-color:green;
}
.div3{
background-color:blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
<div class="container">
<div class="ins div1">div-1</div>
<div class="ins div2">div-2</div>
<div class="ins div3">div-3</div>
</div>
</div>
<button class="prev">prev</button>
<button class="next">next</button>
EDIT : After the updated question
First problem is that you are adding your <style>....</style>
in the body
. You should add it in head
.
Second problem is that you are adding <script>..</script>
before your html content. All the scripts should be in the last of your body. Just before ending body tag, that is </body>
Another solution to the second problem is wrapping your javascript code in $(document).ready(function(){ Insert the code here });
. I have done that in the below snippet.
<!DOCTYPE html>
<html>
<!-- Thanks to kittyCat at stackoverflow.com for helping me with this website.-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>TITLE</title>
<meta name="Title" content="Main">
<style>
.row{
border:1px solid black;
height:100px;
margin:0;
width:100px;
padding:0;
display:block;
position:relative;
overflow:hidden;
}
.container{
height:100px;
margin:0;
width:300px;
padding:0;
display:block;
position:absolute;
left:-100px;
top:0;
-webkit-transition:left 0.5s ease-in-out;
-moz-transition: left 0.5s ease-in-out;
transition: left 0.5s ease-in-out;
}
.ins{
width:100px;
float:left;
height:100px;
margin:0;
padding:0;
background-color:red;
}
.div1 {
background-color: red;
}
.div2{
background-color:green;
}
.div3{
background-color:blue;
}
</style>
</head>
<body>
<div class="row">
<div class="container">
<div class="ins div1">div-1</div>
<div class="ins div2">div-2</div>
<div class="ins div3">div-3</div>
</div>
</div>
<button class="prev">prev</button>
<button class="next">next</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".next").click(function(){
var current = $(".container").css("left");
if(current == "-100px"){
current="-200px";
}
else if(current == "0px"){
current="-100px";
}
$(".container").css("left",current);
});
$(".prev").click(function(){
var current = $(".container").css("left");
if(current == "-100px"){
current="0px";
}
else if(current == "-200px"){
current="-100px";
}
$(".container").css("left",current);});
});
</script>
</body>
</html>
Post a Comment for "CSS3 Animations Slide-In Div"