2017-06-19 1 views
-1

Ich habe einen Code geschrieben, wo ich eine Box habe und wenn ich auf den Button klicken, zeigt es tatsächlich mehr oder weniger Info. Sehen sie in Aktion hier: https://jsfiddle.net/075tcezL/zeigen mehr info wenn klicken sie auf eine taste html, css, javascript

/css 
#model { 
width: 300px; 
border: 1px solid #c1c1c1; 
margin: 20px 10px; 
padding: 0 5px; 
float: left; 
max-height: 300px; 
overflow: hidden; 


-webkit-transition: max-height 0.7s; 
-moz-transition: max-height 0.7s; 
transition: max-heigth 0.7s; 

} 
#show-more { 
display: block; 
background-color: #75868E; 
width: 100px; 
font-size: 12px; 
text-transform: uppercase; 
display: inline-block; 
margin: 20px auto; 
cursor: pointer; 
height: 15px; 
padding: 10px 0; 
} 

#model.open { 
max-height: 1000px; 

-webkit-transition: max-heigth 0.7s; 
-moz-transition: max-heigth 0.7s; 
transition: max-heigth 0.7s; 
} 

// HTML

<body> 
<div id="model" class="model1"> 
    <h3>Series 3</h3> 
     <a href="#series3" id="sseries"><img class="image" 
      src="image"></a> 

<p>Text to hide or show.</p> 

</div>      
<a id="show-more" class="show">Show More</a> 

<script src="javascript.js"></script> 

</body> 

// Das funktioniert mit einem Kasten

var content = document.getElementById("model"); 
var button = document.getElementById("show-more") 

button.onclick = function(){ 

if(content.className == "open"){ 
    content.className = ""; 
    button.innerHTML = "More"; 

} else { 
    content.className = "open"; 
    button.innerHTML = "Less"; 
} 

}; 

aber wenn ich drei Boxen, wie dies - https://jsfiddle.net/jw7pfb1w/ Wie öffne oder schließe ich sie gleichzeitig, wenn ich auf diesen Knopf klicke?

Jede Hilfe wäre willkommen!

+0

Sie benötigt hier Ihr minimales Beispiel für das Problem Code zu platzieren, kein jsfiddle, die in Zukunft helfen, niemanden ändern oder verschwinden die Zukunft: https://stackoverflow.com/help/mcve – Rob

+0

Hinzugefügt Problemcode. –

Antwort

-1

Sie document.getElementsByClassName('.model1'); können alle Elemente auf der Seite mit der gleichen Klasse wählen:

var content = document.getElementsByClassName("model1"); 
var button = document.getElementById("show-more"); 

button.onclick = function(){ 
    for (i = 0; i < content.length; ++i) {  
     if(content[i].className == "model1 open"){ 
     content[i].className = "model1"; 
     button.innerHTML = "More"; 
    } else { 
     content[i].className = "model1 open"; 
     button.innerHTML = "Less"; 
    } 
    } 
}; 

Sie brauchen auch die CSS für open in Ihrem 3-Panel Beispiel:

div.open { 
    max-height: 1000px; 
    -webkit-transition: max-heigth 0.7s; 
    -moz-transition: max-heigth 0.7s; 
    transition: max-heigth 0.7s; 
} 

Added eine funktionierende Geige für dich: https://jsfiddle.net/jw7pfb1w/5/

+0

Für Sie aktualisiert. Vielleicht möchten Sie in jQuery nachsehen, da dies diese Art von Benutzeroberflächenfunktionalität erheblich vereinfacht. – mjw

0

Sie können die gleiche Methode dreimal verwenden oder eine Schleife machen.

Um dies zu erreichen, können Sie in Ihrem HTML mehrere IDs mit Zahlen platzieren. Wie model1, model2, model3, ...

var button = document.getElementById("show-more") 
 

 
button.onclick = function(){ 
 

 
\t // we loop three times for three blocks 
 
\t for (var i = 0; i < 3; i++) { 
 
    // i + 1 -> 1, 2, 3 
 
    // so model1, then model2, then model3 
 

 
    // using concatenation string "model" + the number 
 
    var content = document.getElementById("model" + (i + 1)); 
 

 
    // using toggle function will add the class if not present, else remove it. 
 
    var isOpen = content.classList.toggle('open'); 
 

 
    button.innerHTML = isOpen ? "More" : "Less"; 
 
    } 
 

 
};
.model { 
 
\t width: 100px; 
 
\t border: 1px solid #c1c1c1; 
 
\t margin: 20px 10px; 
 
\t padding: 0 5px; 
 
\t display: inline-block; 
 
\t max-height: 100px; 
 
\t overflow: hidden; 
 
} 
 

 
.open { 
 
\t max-height: 1000px !important; \t 
 
}
<button id="show-more">More</button> 
 
<div> 
 
    <div class="model" id="model1">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div> 
 
    <div class="model" id="model2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div> 
 
    <div class="model" id="model3">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div> 
 
</div>

+0

Vielen Dank! :)) Das funktioniert ganz gut –

Verwandte Themen