IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

jQuery Discussion :

[JQuery] div qui s'ouvrent avec un effet slide, faire que le "+" devienne un '-'


Sujet :

jQuery

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 8
    Points : 10
    Points
    10
    Par défaut [JQuery] div qui s'ouvrent avec un effet slide, faire que le "+" devienne un '-'
    Bonsoir à tous les développeurs du site,

    Sur un site, dans le corps sont inclus plusieurs div. Ceux-ci "s'ouvrent" et "se ferment" en cliquant sur un lien. Cela se fait grâce au plugin animatedcollapse de JQuery avec toggle. J'aimerais que si le div soit affiché, le lien pour le fermer soit un petit "-". Et si le div est caché, que le lien pour l'ouvrir soit un petit "+". Avec un petit script JS tout simple; c'est facile il suffit de rajouter un innerHTML.
    Mais là avec le script JQuery je bloque.

    Voici un bout de code qui contient le lien pour cacher/afficher un div et le div:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    <h4><a href="javascript:animatedcollapse.toggle('p_1');"><span id="a_p_1"><img alt="Cacher" src="images/corps/moins.png"/></span></a> Titre :</h4>
    <p id="p_1">Contenu du div
    </p>
    Et voici le script animatedcollapse.js :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
     
    var animatedcollapse={
    divholders: {}, //structure: {div.id, div.attrs, div.$divref}
    divgroups: {}, //structure: {groupname.count, groupname.lastactivedivid}
    lastactiveingroup: {}, //structure: {lastactivediv.id}
     
    show:function(divids){ //public method
    	if (typeof divids=="object"){
    		for (var i=0; i<divids.length; i++)
    			this.showhide(divids[i], "show")
    	}
    	else
    		this.showhide(divids, "show")
    },
     
    hide:function(divids){ //public method
    	if (typeof divids=="object"){
    		for (var i=0; i<divids.length; i++)
    			this.showhide(divids[i], "hide")
    	}
    	else
    		this.showhide(divids, "hide")
    },
     
    toggle:function(divid){ //public method
    	this.showhide(divid, "toggle")
    },
     
    addDiv:function(divid, attrstring){ //public function
    	this.divholders[divid]=({id: divid, $divref: null, attrs: attrstring})
    	this.divholders[divid].getAttr=function(name){ //assign getAttr() function to each divholder object
    		var attr=new RegExp(name+"=([^,]+)", "i") //get name/value config pair (ie: width=400px,)
    		return (attr.test(this.attrs) && parseInt(RegExp.$1)!=0)? RegExp.$1 : null //return value portion (string), or 0 (false) if none found
    	}
    },
     
    showhide:function(divid, action){
    	var $divref=this.divholders[divid].$divref //reference collapsible DIV
    	if (this.divholders[divid] && $divref.length==1){ //if DIV exists
    		var targetgroup=this.divgroups[$divref.attr('groupname')] //find out which group DIV belongs to (if any)
    		if ($divref.attr('groupname') && targetgroup.count>1 && (action=="show" || action=="toggle" && $divref.css('display')=='none')){ //If current DIV belongs to a group
    			if (targetgroup.lastactivedivid && targetgroup.lastactivedivid!=divid) //if last active DIV is set
    				this.slideengine(targetgroup.lastactivedivid, 'hide') //hide last active DIV within group first
    				this.slideengine(divid, 'show')
    			targetgroup.lastactivedivid=divid //remember last active DIV
    		}
    		else{
    			this.slideengine(divid, action)
    		}
    	}
    },
     
    slideengine:function(divid, action){
    	var $divref=this.divholders[divid].$divref
    	if (this.divholders[divid] && $divref.length==1){ //if this DIV exists
    		var animateSetting={height: action}
    		if ($divref.attr('fade'))
    			animateSetting.opacity=action
    		$divref.animate(animateSetting, $divref.attr('speed')? parseInt($divref.attr('speed')) : 500)
    		return false
    	}
    },
     
    generatemap:function(){
    	var map={}
    	for (var i=0; i<arguments.length; i++){
    		if (arguments[i][1]!=null){
    			map[arguments[i][0]]=arguments[i][1]
    		}
    	}
    	return map
    },
     
    init:function(){
    	var ac=this
    	jQuery(document).ready(function($){
    		var persistopenids=ac.getCookie('acopendivids') //Get list of div ids that should be expanded due to persistence ('div1,div2,etc')
    		var groupswithpersist=ac.getCookie('acgroupswithpersist') //Get list of group names that have 1 or more divs with "persist" attribute defined
    		if (persistopenids!=null) //if cookie isn't null (is null if first time page loads, and cookie hasnt been set yet)
    			persistopenids=(persistopenids=='nada')? [] : persistopenids.split(',') //if no divs are persisted, set to empty array, else, array of div ids
    		groupswithpersist=(groupswithpersist==null || groupswithpersist=='nada')? [] : groupswithpersist.split(',') //Get list of groups with divs that are persisted
    		jQuery.each(ac.divholders, function(){ //loop through each collapsible DIV object
    			this.$divref=$('#'+this.id)
    			if ((this.getAttr('persist') || jQuery.inArray(this.getAttr('group'), groupswithpersist)!=-1) && persistopenids!=null){
    				var cssdisplay=(jQuery.inArray(this.id, persistopenids)!=-1)? 'block' : 'none'
    			}
    			else{
    				var cssdisplay=this.getAttr('hide')? 'none' : null
    			}
    			this.$divref.css(ac.generatemap(['height', this.getAttr('height')], ['display', cssdisplay]))
    			this.$divref.attr(ac.generatemap(['groupname', this.getAttr('group')], ['fade', this.getAttr('fade')], ['speed', this.getAttr('speed')]))
    			if (this.getAttr('group')){ //if this DIV has the "group" attr defined
    				var targetgroup=ac.divgroups[this.getAttr('group')] || (ac.divgroups[this.getAttr('group')]={}) //Get settings for this group, or if it no settings exist yet, create blank object to store them in
    				targetgroup.count=(targetgroup.count||0)+1 //count # of DIVs within this group
    				if (!targetgroup.lastactivedivid && this.$divref.css('display')!='none' || cssdisplay=="block") //if this DIV was open by default or should be open due to persistence								
    					targetgroup.lastactivedivid=this.id //remember this DIV as the last "active" DIV (this DIV will be expanded)
    				this.$divref.css({display:'none'}) //hide any DIV that's part of said group for now
    			}
    		}) //end divholders.each
    		jQuery.each(ac.divgroups, function(){ //loop through each group
    			if (this.lastactivedivid)
    				ac.divholders[this.lastactivedivid].$divref.show() //and show last "active" DIV within each group (one that should be expanded)
    		})
    		var $allcontrols=$('*[rel]').filter('[@rel^="collapse-"], [@rel^="expand-"], [@rel^="toggle-"]') //get all elements on page with rel="collapse-", "expand-" and "toggle-"
    		var controlidentifiers=/(collapse-)|(expand-)|(toggle-)/
    		$allcontrols.each(function(){
    			$(this).click(function(){
    				var relattr=this.getAttribute('rel')
    				var divid=relattr.replace(controlidentifiers, '')
    				var doaction=(relattr.indexOf("collapse-")!=-1)? "hide" : (relattr.indexOf("expand-")!=-1)? "show" : "toggle"
    				return ac.showhide(divid, doaction)
    			}) //end control.click
    		})// end control.each
    		$(window).bind('unload', function(){
    			ac.uninit()
    		})
    	}) //end doc.ready()
    },
     
    uninit:function(){
    	var opendivids='', groupswithpersist=''
    	jQuery.each(this.divholders, function(){
    		if (this.$divref.css('display')!='none'){
    			opendivids+=this.id+',' //store ids of DIVs that are expanded when page unloads: 'div1,div2,etc'
    		}
    		if (this.getAttr('group') && this.getAttr('persist'))
    			groupswithpersist+=this.getAttr('group')+',' //store groups with which at least one DIV has persistance enabled: 'group1,group2,etc'
    	})
    	opendivids=(opendivids=='')? 'nada' : opendivids.replace(/,$/, '')
    	groupswithpersist=(groupswithpersist=='')? 'nada' : groupswithpersist.replace(/,$/, '')
    	this.setCookie('acopendivids', opendivids)
    	this.setCookie('acgroupswithpersist', groupswithpersist)
    },
     
    getCookie:function(Name){ 
    	var re=new RegExp(Name+"=[^;]*", "i"); //construct RE to search for target name/value pair
    	if (document.cookie.match(re)) //if cookie found
    		return document.cookie.match(re)[0].split("=")[1] //return its value
    	return null
    },
     
    setCookie:function(name, value, days){
    	if (typeof days!="undefined"){ //if set persistent cookie
    		var expireDate = new Date()
    		expireDate.setDate(expireDate.getDate()+days)
    		document.cookie = name+"="+value+"; path=/; expires="+expireDate.toGMTString()
    	}
    	else //else if this is a session only cookie
    		document.cookie = name+"="+value+"; path=/"
    }
     
    }
    J'ai fais quelques essais, mais sans succès, c'est pourquoi je sollicite votre aide.
    Merci beaucoup d'avance,
    A+

  2. #2
    Rédacteur

    Avatar de Bovino
    Homme Profil pro
    Développeur Web
    Inscrit en
    Juin 2008
    Messages
    23 647
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2008
    Messages : 23 647
    Points : 91 220
    Points
    91 220
    Billets dans le blog
    20
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <a href="javascript:animatedcollapse.toggle('p_1');$('#p_1').innerHTML=$('#p_1').innerHTML=='+'?'-':'+'">
    Devrait fonctionner je pense.

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 8
    Points : 10
    Points
    10
    Par défaut
    Re,

    Celle ligne de coda affiche "-" ou "+" dans une nouvelle page, comme lors d'un document.write qui s'effectue après chargement de la page..
    Merci beaucoup

  4. #4
    Membre à l'essai
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 8
    Points : 10
    Points
    10
    Par défaut
    Quelqu'un a une idée ?
    Merci,
    A+

Discussions similaires

  1. Animation Javascript/Jquery Div qui s'écartent
    Par BeUnderway dans le forum jQuery
    Réponses: 2
    Dernier message: 29/10/2014, 10h07
  2. Gérer plusieurs onglets avec l'effet Slide
    Par Zadoner dans le forum jQuery
    Réponses: 4
    Dernier message: 06/05/2012, 15h31
  3. Afficher un div avec un effet de slide en cochant un bouton radio
    Par zbibounette dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 20/07/2008, 10h55
  4. Problème avec un div qui n'est pas pris en compte
    Par boss_gama dans le forum Général JavaScript
    Réponses: 11
    Dernier message: 25/07/2006, 16h32

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo