用js实现简单的图片轮播功能:
//forTese.js
'use strict'
//自动轮播功能
var count=0,
timer,
isgo = true;
var ul_img = document.getElementsByClassName("ul_img");
var li_img = document.getElementsByClassName("li_img");
function showtime() {
timer = setInterval(function(){
if(isgo){
count++;
ul_img[0].style.transform = "translateX("+ -400*count +"px)";
if(count >= li_img.length-1){
count=li_img.length-1;
isgo = false;
}
}else{
count--;
ul_img[0].style.transform = "translateX("+ -400*count +"px)";
if(count <= 0){
count =0;
isgo = true;
}
}
},4000);
}
showtime();
//点击左右切换功能
var arrow = document.getElementsByClassName("arrow");
for(var i = 0; i<arrow.length; i++){
arrow[i].onmouseover = function () {
clearInterval(timer);
}
arrow[i].onmouseout = function () {
showtime();
}
arrow[i].onclick = function(){
//点击向右
if(this.title == "1") {
if (count == 0) count = 3;
else count--;
}else{
if(count == 3) count = 0;
else count ++;
}
ul_img[0].style.transform = "translateX("+ -400*count +"px)";
}
}
//下面标签条对应图片功能
var div_btn = document.getElementsByClassName("div_btn");
div_btn[0].style.backgroundColor="aqua";
for(var i = 0;i<div_btn.length;i++){
div_btn[i].index = i;
div_btn[i].onmouseover = function () {
clearInterval(timer);
for( var j = 0;j<div_btn.length;j++){
div_btn[j].style.backgroundColor="aquamarine";
}
div_btn[this.index].style.backgroundColor="aqua";
count = this.index;
if(count == 3) isgo=false;
if(count == 0) isgo=true;
ul_img[0].style.transform = "translateX("+ -400*count +"px)";
}
div_btn[i].onmouseout = function () {
showtime();
}
}
forTest.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="./jquery-3.2.1.js"></script>
<link rel="stylesheet" href="./forTest.css" type="text/css">
<title>Title</title>
</head>
<body>
<div class="main_div">
<div class="arrows">
<span title="1" class="arrow"><</span>
<span title="0" class="arrow" style="float:right;">></span>
</div>
<ul class="ul_img">
<li class="li_img"><img src="./img/1.jpg" ></li>
<li class="li_img"><img src="./img/2.jpg" ></li>
<li class="li_img"><img src="./img/3.jpg" ></li>
<li class="li_img"><img src="./img/4.jpg" ></li>
</ul>
</div>
<div style="margin-left:550px;">
<div class="div_btn"></div>
<div class="div_btn"></div>
<div class="div_btn"></div>
<div class="div_btn"></div>
</div>
</body>
<script src="forTest.js" type="text/javascript"></script>
</html>
forTest.css
.main_div{
width:400px;
overflow:hidden;
position:relative;
left:500px;
top:30px;
}
.ul_img{
width:6000px;
padding:0px;
margin:0px;
transition:all 2s;
}
.li_img{
width:400px;
height:550px;
float:left;
list-style:none;
}
img{
width:100%;
}
.arrows {
z-index: 9999;
position: absolute;
padding-top: 250px;
width: 400px;
}
.arrows span {
font-size: 3em;
color: seashell;
}
.arrows span:hover {
/*变小手*/
cursor: pointer;
background-color: rgba(192, 192, 192, 0.29);
}
.div_btn {
float: left;
border-radius: 100px;
background-color: aquamarine;
width: 60px;
height: 10px;
margin-left: 10px;
margin-top: 40px;
}
.div_btn:hover {
background-color: aqua;
}
function animate(obj,json,fn) { // 第一参数 动的对象 2 attr 动的那个属性 3 属性值少多少 clearInterval(obj.timer); obj.timer = setInterval(function() { var flag = true; // 用来判断是否停止定时器 定时器的里面 // 每隔 30 毫秒就要运行一次 for(var k in json) { // k 属性 json[k] 是属性值 // 第一 k 是 width // 第二 k height // leader = 600 - 100 // leader = leader + (target - leader ) /10 ; // 计算步长 // target 目标位置 leader 不清楚,我们不知道用户改那个属性 // 检测用户给我们了什么属性, 我们就用这个属性的值 来计算 // 我们怎么知道用户给我们属性,我们怎么又能得到属性的值呢? // var leader = obj.style[attr]; 他只能得到行内式 var leader = 0; // 因为透明度是 小数 取值 0~1 之间 第二个 它没有单位 if(k == "opacity") { // 先解决小数的问题 leader = Math.round(getStyle(obj,k) * 100) || 100; // 如果没有opacity 默认当 1 看 1* 100 = 100 // 我们去过来的是 0.3 但是小数不好计算 我们乘以100 30 好计算 } else { leader = parseInt(getStyle(obj,k)) || 0; // 他本身有单位 所以要去掉 默认的默认是 0 } //去掉px 的方法 parseInt(25px) 25 /* alert(leader);*/ var step = (json[k] - leader) / 10; step = step > 0 ? Math.ceil(step) : Math.floor(step); leader = leader + step; if( k == "opacity") { // 记住我们的透明是不加单位的 不要加px obj.style.opacity = leader / 100; // opacity: 0.4; // filter: alpha(opacity = 40); /*不能改只能是 40 不能是 0.4*/ obj.style.filter = "alpha(opacity = "+leader+")"; } // 设置层级 else if(k == "zIndex") { obj.style.zIndex = json[k]; // 直接给值 } else { obj.style[k] = leader + "px"; // 其他的要单位 } if(leader != json[k]) { // 只要三个属性有一个没有到达,我就取反 // 这句话要写到 for in 里面 因为有三个属性,所以用 for in 才能遍历 // 三个都到了 都不执行这句话了 flag = false; } } // console.log(flag); if(flag) { clearInterval(obj.timer); if(fn){ // 如果有函数传递过来 , 定时器结束了,说明动画结束 可以执行 回调函数 fn(); // 执行函数 fn + (); } } },30) }
用css实现的一个简单轮播图。 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>简易轮播图效果</title> <style type="text/css"> * { margin: 0; padding: 0; font: normal 12px/1.5em Verdanda,Lucida, Arial, Helvetica, "宋体"; } .wapper { width: 500px; height: 500px; border: 1px solid #333333; background: #CCCCCC; text-align: center; } .content { width: 480px; height: 460px; margin: 10px auto 0; overflow: hidden; border: 1px solid #999999; background: #FFFFFF; } .box { float: left; width: 460px; height: 450px; margin-bottom: 10px; padding: 10px; overflow: auto; } .pagger { width: 480px; height: 20px; margin: 5px auto; text-align: right; } .pagger a { padding: 2px 4px; text-decoration: none; color: #FFFFFF; background: #FF0000; } .pagger a:hover { color: #000000; background: #FFFFFF } </style> </head> <body> <div class="wapper"> <div class="content"> <div class="box" id="a1"> <h2>幻灯片的简单模拟-使用CSS</h2> <p>第一屏</p> </div> <div class="box" id="a2"> <p>第二屏</p> </div> <div class="box" id="a3"> <p>第三屏</p> </div> <div class="box" id="a4"> <p>第四屏</p> </div> <div class="box" id="a5"> <p>第五屏</p> <p>第五屏</p> <p>第五屏</p> <p>第五屏</p> </div> </div> <div class="pager"> <a href="#a1">1</a> <a href="#a2">2</a> <a href="#a3">3</a> <a href="#a4">4</a> <a href="#a5">5</a> </div> </div> </body> </html>
function $( v ){ if( typeof v === 'function' ){ window.onload = v; } else if ( typeof v === 'string' ) { return document.getElementById(v); } else if ( typeof v === 'object' ) { return v; } } function getStyle( obj, attr ){ return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle( obj )[attr]; } function addZ(iNum){ return iNum<10 ? "0"+iNum : ""+iNum; } function doMove(obj,speed,end,attr,endFn){ var oldAttr = parseInt(getStyle(obj,attr)); oldAttr>end ? speed = -speed : speed = speed; clearInterval(obj.timer); obj.timer = setInterval(function(){ var newAttr = parseInt(getStyle(obj,attr)) + speed; if(newAttr>=end&&speed>0||newAttr<=end&&speed<0){ clearInterval(obj.timer); obj.style[attr] = end+"px"; //if(endFn){endFn();} endFn && endFn(); }else{ obj.style[attr] = newAttr +"px"; } },30);