2017-06-19 148 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

非常感谢! :))这工作得很好 –

相关问题