轮播图

轮播图1

轮播图2

轮播图3

轮播图4


轮播图


<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>图片轮播的两种方式</title>
	<style>
		#controls {
			width:400px;
			margin: auto;
			text-align: center;
		}
		#container {
			width: 400px;
			height:400px;
			border: 10px solid #eee;
			position: relative;
			background: gray;
			margin: 10px auto 0;
		}
		#prev, #next {
			position: absolute;
			background: black;
			filter:alpha(opacity:40);
			opacity: 0.4;
			font-size: 20px;
			color: white;
			width: 40px;
			height: 40px;
			border: 2px solid white;
			line-height: 40px;
			text-align: center;
			top: 180px;
			pointer: cursor;
			text-decoration: none;
		}
		#prev:hover, #next:hover {
			filter:alpha(opacity:80);
			opacity: 0.8;
		}
		#order, #info {
			position: absolute;
			width:100%;
			height: 30px;
			line-height: 30px;
			text-align: center;
			background: black;
			filter:alpha(opacity:60);
			opacity: 0.6;
			font-size: 20px;
			color: white;
		}
		#prev {
			left: 0;
		}
		#next {
			right: 0;
		}
		#picture {
			height: 400px;
			width: 400px;
		}
		#order {
			top: 0;
		}
		#info {
			bottom: 0;
		}
	</style>
	
		
</head>
<body>
	<div id="controls">
		<input id="round" type="button" value = "循环播放">
		<input id="single" type="button" value = "顺序播放">
	</div>
	<div id="container">
        <a href='javascript:' id="prev">&lt;</a>
        <a href='javascript:' id="next">&gt;</a>
        <div id="order">图片加载中……</div>
        <div id="info">图片加载中……</div>
        <img id="picture">
	</div>
	<script type="text/javascript">
	    // 封装一个函数  根据id找元素
	    function $(id) {
	    	return document.getElementById(id);
	    }
	    // 定义一个数组 存放图片路径
	    var imgsArr = ["6.jpg","7.jpg","8.jpg","9.jpg"];
	    var imgsText = ["图片一","图片二","图片三","图片四"];
	    var index = 0;
        var flag = true;// 值为true 默认为顺序播放 
        // 定义显示信息
        function showInfo() {
        	$("picture").src = imgsArr[index];
        	$("order").innerHTML = (index+1)+"/4";
        	$("info").innerHTML = imgsText[index];
        }
        showInfo();

	    // 给左箭头绑定事件
        $("prev").onclick = function() {
              index--;
              // 是顺序播放 并且index为-1
              if (flag && index === -1) {
              	   alert("已经是第一张了");
              	   index = 0;
              } else if( !flag && index === -1) {
                   index = imgsArr.length - 1;
              }
              showInfo();
        } 

        // 给右箭头绑定事件
        $("next").onclick = function() {
        	index++;
        	// 顺序播放
        	if (flag && index === imgsArr.length) {
        		alert("已经是最后一张了");
        		index = imgsArr.length - 1;
        	} else if (!flag && index === imgsArr.length) {// 循环播放
                index = 0;
        	}
        	showInfo();
        }

        $("round").onclick = function() {
        	alert("开始循环播放了");
        	flag = false;
        }

        $("single").onclick = function() {
        	alert("开始顺序播放了");
        	flag = true;
        }
	</script>
</body>
</html>
返回顶层目录

轮播图二

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	 <img src="img/6.jpg" alt="">
	 <script type="text/javascript">
	    var oImg = document.getElementsByTagName("img")[0];
	    var imgArr = ["6.jpg","7.jpg","8.jpg","9.jpg"];
	    var index = 0;
	    setInterval(function() {
    		index++;
    		if (index == imgArr.length) {
    			index = 0;
    		}
    		oImg.src = "img/"+imgArr[index];
	    }, 1000);
	 </script>
</body>
</html>
返回顶层目录

轮播图3 —— 淡入与淡出

<!DOCTYPE html>
<html>

<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }
        
        ul,
        ol {
            list-style: none;
        }
        
        .wrapper {
            width: 670px;
            height: 240px;
            margin: 100px auto;
            overflow: hidden;
            position: relative;
        }
        
        ul {
            position: relative;
        }
        
        ul li {
            position: absolute;
            top: 0;
            left: 0;
        }
        
        ol {
            position: absolute;
            right: 0;
            bottom: 10px;
            width: 190px;
        }
        
        ol li {
            float: left;
            width: 20px;
            height: 20px;
            margin: 0 5px;
            text-align: center;
            border-radius: 50%;
            cursor: default;
            background-color: #abc;
        }
        
        ol li.current {
            background-color: pink;
        }
    </style>
</head>

<body>
    <div class="wrapper">
        <ul id="box">
            <li style="z-index: 6;"><img src="images/1.jpg" alt="" /></li>
            <li style="z-index: 5;"><img src="images/2.jpg" alt="" /></li>
            <li style="z-index: 4;"><img src="images/3.jpg" alt="" /></li>
            <li style="z-index: 3;"><img src="images/4.jpg" alt="" /></li>
            <li style="z-index: 2;"><img src="images/5.jpg" alt="" /></li>
            <li style="z-index: 1;"><img src="images/6.jpg" alt="" /></li>
        </ul>
        <ol style="z-index: 10;" id="uu">
            <li class="current">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
            <li>6</li>
        </ol>
    </div>
</body>

</html>
<script src="sport.js"></script>
<script type="text/javascript">
    function $(id) {
        return document.getElementById(id);
    }
    var index = 0;
    var olist = $("box").children;
    var ulist = $("uu").children;
    var timer = setInterval(autoPlay, 1500);

    function autoPlay() {
        index++;
        if (index === olist.length) {
            index = 0;
        }
        for (var i = 0; i < ulist.length; i++) {
            ulist[i].className = "";
            startMove(olist[i], 0, "opacity");
        }
        startMove(olist[index], 1, "opacity");
        ulist[index].className = "current";
    }
</script>
返回顶层目录

sport.js

function startMove(obj,target,attr) {
    clearInterval(obj.timer);
    obj.timer = setInterval(function() {
        var current = parseFloat(getStyle(obj,attr));
        var speed = 0;
        if (attr === "opacity") {
            speed = target - current > 0 ? 0.1: -0.1;
        } else {
            speed = (target - current)/10; //    
            speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
        }
            //  
        if (current === target) {
            clearInterval(obj.timer);
        } else {
            if (attr === "opacity") {
                obj.style[attr] = current + speed
            } else {
               obj.style[attr] = current + speed + "px";
            }   
        }
    },20);
}


function getStyle(obj,attr) {
    if (window.getComputedStyle) {
        return window.getComputedStyle(obj,false)[attr];
    } else {
        return obj.currentStyle[attr];
    }
}

返回顶层目录

轮播图4 —— 左右焦点图

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <style type="text/css">
        body,
        ul,
        ol,
        li,
        img {
            margin: 0;
            padding: 0;
            list-style: none;
        }
        
        #box {
            width: 490px;
            height: 170px;
            padding: 5px;
            position: relative;
            border: 1px solid #ccc;
            margin: 100px auto 0;
            overflow: hidden;
        }
        
        .ad {
            width: 490px;
            height: 170px;
            overflow: hidden;
            position: relative;
        }
        
        #box img {
            width: 490px;
        }
        
        .ad ol {
            position: absolute;
            right: 10px;
            bottom: 10px;
        }
        
        .ad ol li {
            width: 20px;
            height: 20px;
            line-height: 20px;
            border: 1px solid #ccc;
            text-align: center;
            background: #fff;
            float: left;
            margin-right: 10px;
            cursor: pointer;
            _display: inline;
        }
        
        .ad ol li.current {
            background: yellow;
        }
        
        .ad ul li {
            float: left;
        }
        
        .ad ul {
            position: absolute;
            top: 0;
            width: 2940px;
        }
        
        .ad ul li.current {
            display: block;
        }
        
        #arr {
            display: block;
        }
        
        #arr span {
            width: 40px;
            height: 40px;
            position: absolute;
            left: 5px;
            top: 50%;
            margin-top: -20px;
            background: #000;
            cursor: pointer;
            line-height: 40px;
            text-align: center;
            font-weight: bold;
            font-family: '黑体';
            font-size: 30px;
            color: #fff;
            opacity: 0.3;
            border: 1px solid #fff;
        }
        
        #arr #right {
            right: 5px;
            left: auto;
        }
    </style>
</head>

<body>
    <div id="box" class="all">
        <div class="ad">
            <ul id="imgs">
                <li><img src="images/1.jpg" /></li>
                <li><img src="images/2.jpg" /></li>
                <li><img src="images/3.jpg" /></li>
                <li><img src="images/4.jpg" /></li>
                <li><img src="images/5.jpg" /></li>
            </ul>
        </div>
        <div id="arr"><span id="left"></span><span id="right">></span></div>
    </div>
</body>

</html>
<script src="sport.js"></script>
<script type="text/javascript">
    function $(id) {
        return document.getElementById(id);
    }
    var oLeft = $("left");
    var oRight = $("right");
    var oUl = $("imgs");
    var target = 0;
    oRight.onclick = function() {
        if (target === -490 * 4) {
            target === -490 * 4;
        } else {
            target -= 490;
        }
        // 执行运动函数
        startMove(oUl, target, "left");
    }

    oLeft.onclick = function() {
        if (target === 0) {
            target === 0;
        } else {
            target += 490;
        }
        // 执行运动函数
        startMove(oUl, target, "left");
    }
</script>
返回顶层目录

sport.js

function startMove(obj,target,attr) {
    clearInterval(obj.timer);
    obj.timer = setInterval(function() {
        var current = parseFloat(getStyle(obj,attr));
        var speed = 0;
        if (attr === "opacity") {
            speed = target - current > 0 ? 0.1: -0.1;
        } else {
            speed = (target - current)/10; //    
            speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
        }
            //  
        if (current === target) {
            clearInterval(obj.timer);
        } else {
            if (attr === "opacity") {
                obj.style[attr] = current + speed
            } else {
               obj.style[attr] = current + speed + "px";
            }   
        }
    },20);
}


function getStyle(obj,attr) {
    if (window.getComputedStyle) {
        return window.getComputedStyle(obj,false)[attr];
    } else {
        return obj.currentStyle[attr];
    }
}

返回顶层目录

返回目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值