《javaScript100例|03》自写javaScript+CSS轮显效果
Posted 孙叫兽
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《javaScript100例|03》自写javaScript+CSS轮显效果相关的知识,希望对你有一定的参考价值。
目录
源码地址:自写Js+CSS轮显效果.rar-互联网文档类资源-CSDN下载https://download.csdn.net/download/weixin_41937552/37380520
效果图
代码示例
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>JS图片轮显切换</title>
<style type="text/css">
*{ margin:0; padding:0}
#wapper{ position:relative;height:280px; width:316px; overflow:hidden}
#slideshow{width:316px; height:300px; overflow:hidden; position:relative;}
#imglist{width:1264px; height:100%; float:left; padding:0; margin:0;position:absolute; left:0}
#imglist li{ width:25%;height:100%; float:left}
#nextlist{position:absolute; z-index:9}
#nextlist li{ float:left; display:inline; height:30px; padding:10px 0; width:79px;}
#nextlist li.current{background:#c00}
#bg{ position:absolute; z-index:1;background:#ff0;filter:alpha(opacity=20);opacity:0.2; height:50px; width:316px}
</style>
</head>
<body>
<div id="wapper">
<div id="slideshow">
<ul id="imglist">
<li style=" background:#f0c;">1</li>
<li style=" background:#cf0;">2</li>
<li style=" background:#09e;">3</li>
<li style=" background:#efc;">4</li>
</ul>
</div>
<div id="nextlist">
<ul>
<li class="current">111</li>
<li>222</li>
<li>333</li>
<li>444</li>
</ul>
</div>
<div id="bg"></div>
</div>
<script type="text/javascript">
var num = 0;
function nextElement(eleObj,aa){
var obj = document.getElementById(eleObj);
if (obj.movement) {
clearTimeout(obj.movement);
}
if (!obj.style.bottom) {
obj.style.bottom = "-50px";
}
var xpos = parseInt(obj.style.bottom);
if (xpos == aa) {
return false;
}
if (xpos > aa) {
var dist = Math.ceil((xpos - aa)/5);
xpos = xpos - dist;
}
if (xpos < aa) {
var dist = Math.ceil((aa - xpos)/5);
xpos = xpos + dist;
}
obj.style.bottom = xpos + "px";
var repeat = "nextElement('"+eleObj+"','"+aa+"')";
obj.movement = setTimeout(repeat,30);
}
function moveElement() {
var elem = document.getElementById("slideshow");
var nextElem = document.getElementById("nextlist");
var imgElem = document.getElementById("imglist");
var lis = elem.getElementsByTagName("li");
var nextLis = nextElem.getElementsByTagName("li");
if (imgElem.movement) {
clearTimeout(imgElem.movement);
}
if (!imgElem.style.left) {
imgElem.style.left = "0";
}
var xpos = parseInt(imgElem.style.left);
var naa = num * -316;
if (xpos == naa) {
if(num==(lis.length - 1) || num>lis.length){
num=0;
return false;
}
else{
num++;
return false;
}
}
for(var i=0;i<nextLis.length;i++){
nextLis[i].className="";
nextLis[num].className="current";
}
if (xpos > naa) {
var dist = Math.ceil((xpos - naa)/4);
xpos = xpos - dist;
}
if (xpos < naa) {
var dist = Math.ceil((naa - xpos)/4);
xpos = xpos + dist;
}
imgElem.style.left = xpos + "px";
var repeat = "moveElement()";
imgElem.movement = setTimeout(repeat,30);
}
function checkMove(){
var elem = document.getElementById("slideshow");
var nextElem = document.getElementById("nextlist");
var imgElem = document.getElementById("imglist");
var bgElem = document.getElementById("wapper");
var lis = elem.getElementsByTagName("li");
var nextLis = nextElem.getElementsByTagName("li");
bgElem.onmouseover=function(){
nextElement("nextlist",0);
nextElement("bg",0);
}
bgElem.onmouseout=function(){
nextElement("nextlist",-50);
nextElement("bg",-50);
}
for(var i=0;i<nextLis.length;i++){
nextLis[i].onmouseover=function(){
clearInterval(changebox);
thisMove(this);
}
nextLis[i].onmouseout=function(){
startMove();
}
lis[i].onmouseover=function(){
clearInterval(changebox);
//thisStop(this);
}
lis[i].onmouseout=function(){
startMove();
}
}
function startMove(){
changebox = setInterval("moveElement()",4000);
}
function thisMove(obj){
for(var i=0;i<nextLis.length;i++){
nextLis[i].className="";
if(nextLis[i]==obj){
nextLis[i].className="current";
num=i;
setTimeout("moveElement()",0);
}
else{
nextLis[i].className="";
}
}
}
}
changebox = setInterval("moveElement()",4000);
checkMove();
</script>
</body>
</html>
源码地址:自写Js+CSS轮显效果.rar-互联网文档类资源-CSDN下载https://download.csdn.net/download/weixin_41937552/37380520
以上是关于《javaScript100例|03》自写javaScript+CSS轮显效果的主要内容,如果未能解决你的问题,请参考以下文章
自写小函数处理 javascript 0.3*0.2 浮点类型相乘问题
Java单体应用 - 架构模式 - 03.设计模式-03.单例模式