HTML / JS水平滚动DIV列表
HTML / JS horizontal scrolling DIV "list"
我想做的是一个水平滚动的div列表就像互联网上几乎所有的大网站一样(比如netflix)。
我试图使它使用一个主DIV,这将是某种容器,第二个DIV持有所有的内容,是在第一个DIV和很多DIV,每个内容模块一个,去第二个DIV内。第二个DIV溢出的部分应该隐藏,并且可以通过移动它(第二个DIV)来显示内容。
这是我能想出的最好的方法,但它仍然不起作用。
这是我的HTML<button onmouseover="left=1" onmouseout="left=0">
<</button>
<div class="container">
<div id="filler" style="left:0px">
<div class="module" style="background:coral;">testing</div>
<div class="module" style="background:lightblue;">testing</div>
<div class="module" style="background:lightgreen;">testing</div>
<div class="module" style="background:salmon;">testing</div>
<div class="module" style="background:lightyellow;">testing</div>
</div>
</div>
<button onmouseover="right=1" onmouseout="right=0">></button>
CSS .container {
height:50px;
width:200px;
overflow:hidden;
}
#filler {
height:50px;
width:250px;
position:relative;
border-radius:10px;
background:crimson;
}
.module {
width:50px;
height:50px;
border-radius:5px;
float:left;
line-height:50px;
text-align:center;
}
JavaScript: var feft = 0
//feft stands for filler left
var right = 0
var left = 0
var loaded = 0
window.onload=function(){
loaded=1
}
function move() {
if(loaded == 1){
if (left == 1 && feft <= 250) {
//left == 1 && feft <= filler width
document.getElementById("filler").style.left = feft + 1
}
if (right == 1 && feft >= 0) {
//right == 1 && feft >= 0
document.getElementById("filler").style.left = feft - 1
} //these IFs tests if the filler should move
feft = document.getElementById("filler").style.left
//this sets the feft variable to what it needs to be for the next run of the function
}}
window.setInterval(move(), 100)
我为你做了一把小提琴。
演示HTML代码
<button onmouseover="left=1" onClick="move(-1)"><</button>
<div class="container">
<div id="filler" style="left:0px">
<div class="module" style="background:coral;">testing</div>
<div class="module" style="background:lightblue;">testing</div>
<div class="module" style="background:lightgreen;">testing</div>
<div class="module" style="background:salmon;">testing</div>
<div class="module" style="background:lightyellow;">testing</div>
</div>
</div>
<button onmouseover="right=1" onClick="move(1)">></button>
JS代码var position = 0;
var moduleCount = document.querySelector(".module").length;
window.move = function(number) {
if (number) {
position += number;
if (number == 0 || number > moduleCount) {
position = 0;
}
} else {
if (position <= 4) {
position++;
} else {
position = 0;
}
}
moduleOffset = document.querySelector(".module").offsetWidth;
filler = document.querySelector("#filler");
filler.style.left = -( position* moduleOffset) + "px";
}
setInterval(window.move, 3000);
你要做的事情叫做"Carousel"。我建议使用bootstrap为例,然后在您的网站实现它。
http://getbootstrap.com/javascript/旋转木马
尝试将overflow: scroll
作为CSS属性添加到您的容器div中。
相关文章:
- 从提示(jQuery)添加带有变量的列表项(Div)
- 使用无序列表展开DIV
- 范围和列表元素在 DIV 中换行
- 使用jQuery将列表中的元素集封装在DIV中
- 如何使用 jQuery 在键盘上为 Div 列表启用箭头导航
- 悬停/单击DIV列表
- 使用 Jquery 克隆下拉列表并附加到动态 Div
- 单击DIV时,如何切换UL列表
- 在动态html列表内容周围添加Div
- JavaScript链式下拉列表显示/隐藏一个Div
- 在Div标签中加载带有选择列表的图像
- 在数据列表中切换Div标签
- 显示“正在加载”Div元素,直到列表已加载,然后隐藏Div元素
- HTML / JS水平滚动DIV列表
- 如何制作新的Div内部列表项目
- 替换iframe与Div((多个)下拉列表相关)
- Javascript:用键盘导航DIV列表
- 如何使列表项显示/隐藏DIV列表或DIV的指定项
- 如何在Div中显示所有的列表标题?
- 可点击下拉列表不可见DIV