11540 sujets

JavaScript, DOM et API Web HTML5

Bonjour,
j'ai un souci sur un élément collapsible qui doit s'ouvrir vers la gauche quand on clique sur un bouton. Il s'ouvre bien mais ne se referme pas Smiley decu
Est-ce que quelqu'un peux m'éclairer ?

Voici le code :

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Horizontal Expand Collapse Using jQuery</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
<script type="text/javascript">
$(document).ready(function() {
	var hideWidth = '-490px'; //width that will be hidden
	var collapsibleEl = $('.collapsible'); //collapsible element
	var buttonEl =  $(".collapsible button"); //button inside element

	collapsibleEl.css({'margin-right': hideWidth}); //on page load we'll move and hide part of elements
	
	$(buttonEl).click(function()
	{
		var curwidth = $(this).parent().offset(); //get offset value of the element
		if(curwidth.right>0) //compare margin-left value
		{
			//animate margin-right value to -490px
			$(this).parent().animate({marginRight: hideWidth}, 300 );
			$(this).html('&laquo;'); //change text of button
		}else{
			//animate margin-right value 0px
			$(this).parent().animate({marginRight: "0"}, 300 );	
			$(this).html('&raquo;'); //change text of button
		}
	});
});

</script>
<style type="text/css">
<!--
.collapsible {
	margin-bottom: 20px;
	background: #EBEBEB;
	border: 1px solid #CCCCCC;
	padding: 10px;
	width: 500px;
	font: 12px Arial, Helvetica, sans-serif;
	color: #333333;
	display: block;
	overflow: hidden;
	float:right;
}
.collapsible button {
	height: 135px;
	float: left;
	border: 1px solid #999999;
	background: #999999;
	color: #CECECE;
	margin-right: 10px;
}
.collapsible button:hover {
	background: #990000;
	border: 1px solid #990000;
}
-->
</style>
</head>
<body>

<div class="collapsible">
<button>&raquo;</button>
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
</div>


</body>
</html>
Bonjour.

Très simple, offset().right n'existe pas (Cf. offset().left) ! Quand j'ai à gérer ce genre d'animation, je rajoute ou enlève une classe selon si le bloc est sorti ou non.

Un truc du genre
Modifié par SolidSnake (07 Oct 2014 - 14:18)
Modérateur
Salut,

Pas trop le temps d'aller en profondeur dans ton script. Je vois des valeurs en dur qui sûrement peuvent être éviter.

Sinon, pour ton soucis :

$(document).ready(function() {
	var hideWidth = '-490px'; //width that will be hidden
	var collapsibleEl = $('.collapsible'); //collapsible element
	var buttonEl =  $(".collapsible button"); //button inside element

	collapsibleEl.css({'margin-right': hideWidth}); //on page load we'll move and hide part of elements
	
	$(buttonEl).click(function()
	{
		$(this).toggleClass('current');
		
		if (!$(this).hasClass('current')) {//animate margin-right value to -490px
			$(this).parent().animate({marginRight: hideWidth}, 300 );
			$(this).html('&laquo;'); //change text of button
		}else{
			//animate margin-right value 0px
			$(this).parent().animate({marginRight: "0"}, 300 );	
			$(this).html('&raquo;'); //change text of button			
		}
	});
});

Modifié par niuxe (07 Oct 2014 - 14:10)