這是虛擬代碼。我的要求是當我點擊最後的li
元素,然後一個hidden div
裏面會顯示。但我需要scroll
下來才能看到它。 所以當我點擊li
與id=hello
然後窗口應該自動向下滾動到該div?如何使用CSS/JS滾動到特定元素
首選使用CSS然後JS和沒有jQuery。
var ele=document.getElementById('hello');
ele.addEventListener("click", function (event) {
document.getElementById("hidden-div").style.display="block";
},false);
.fixed-height-div{
height:120px;
overflow-y:scroll;
}
.fixed-height-div > li{
background-color:lightblue;
list-style-type: none;
}
<div class="fixed-height-div">
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
<li>F</li>
<li>G</li>
<li id="hello">H
<div id="hidden-div" style="display:none;">
This should be displayed after click<br><br>
And the scroll should come to this screen.
</div>
</li>
你需要它是動畫? – 2014-11-06 07:05:02
任何東西。只需要自動滾動到該div。 – 2014-11-06 07:07:34
那麼,我會嘗試的第一件事就是在你的''hello''''裏面放一個簡單的錨元素,並將它的'href'屬性設置爲'#myHiddenDivsId'。它不需要JS。 – 2014-11-06 07:16:25