JQuery controls the sliding of left and right in different directions

  • 2020-03-30 03:33:58
  • OfStack

Introduce jquery.js, copy the following code, can be run.


<style type="text/css"> 
.slide { 
position: relative; 
height: 200; 
lightyellow; 
} 

.slide .inner { 
position: absolute; 
left: 0; 
bottom: 0; 
height: 100; 
lightblue; 
width: 100% 
} 
</style>

1. Slidetoggle () alternate slidedown(),slideup()

The Html code


<div id="slidebottom" class="slide"> 
<button> 
slide it 
</button> 
<div class="inner"> 
Slide from bottom 
</div> 
</div>

Js code


$('#slidebottom button').click(function() { 
$(this).next().slideToggle(); 
});

2. Animate Left

The Html code


<div id="slidewidth" class="slide"> 
<button> 
slide it 
</button> 
<div class="inner"> 
Slide from bottom 
</div> 
</div>

Js code


$("#slidewidth button").click(function(){ 
$(this).next().animate({width: 'toggle'}); 
}); 

3. Animate Left Margin (not hidden)

The Html code


<div id="slideleft" class="slide" style="width: 50%;float: right"> 
<button> 
slide it 
</button> 
<div class="inner"> 
Slide from bottom 
</div> 
</div>

Js code


$("#slideleft button").click(function(){ 
var $lefty = $(this).next(); 
$lefty.animate({ 
left:parseInt($lefty.css('left'),10)==0 ? -$lefty.outerWidth() : 0 
}); 
});

Slide to right

The Html code


<div id="slidemarginleft" class="slide" style="width: 60%;float: left"> 
<button> 
slide it 
</button> 
<div class="inner"> 
Slide from bottom 
</div> 
</div>

Js code


$("#slidemarginleft button").click(function(){ 
var $marginlefty = $(this).next(); 
$marginlefty.animate({ 
marginLeft:parseInt($marginlefty.css('marginLeft'),10)==0 ? $marginlefty.outerWidth() : 0 
}); 
});

Related articles: