2017-06-19 79 views
-1

我寫了一個代碼,其中有一個框,當我單擊該按鈕時,它實際上會顯示更多或更少的信息。看到它在行動這裏:https://jsfiddle.net/075tcezL/單擊按鈕時顯示更多信息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> 

//這適用於一個盒子

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"; 
} 

}; 

但如果我有三個箱子,這樣的 - https://jsfiddle.net/jw7pfb1w/ 點擊該按鈕時,如何同時打開或關閉它們?

任何幫助,將不勝感激!

+0

您需要在這裏放置問題代碼的你最小的例子,不是的jsfiddle可以改變或消失明天幫助中沒有一個未來:https://stackoverflow.com/help/mcve – Rob

+0

增加了問題代碼。 –

回答

-1

您可以使用document.getElementsByClassName('.model1');來選擇頁面上的所有元素具有相同等級:

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"; 
    } 
    } 
}; 

您還需要爲open的CSS在3面板例如:

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

添加一個工作小提琴爲你:https://jsfiddle.net/jw7pfb1w/5/

+0

爲您更新。您可能希望查看jQuery,因爲這可以使這種UI功能更容易。 – mjw

0

你可以使用相同的方法三次,或做一個循環。

爲了達到這個目的,你可以在你的html中添加多個帶數字的id。 像模型1,模型2,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

非常感謝! :))這工作得很好 –