欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

第三章 web前端开发工程师--JavaScript进阶程序设计 3-3 图片切换综合示例

程序员文章站 2022-03-04 19:12:46
...

JavaScript图片切换综合实例

 

本节课所讲内容:

  1. JavaScript 图片切换综合实例

主讲教师:Head老师

一. JavaScript 新闻网站字体控制

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style>
p { 
    margin:0; 
    }
body { 
    text-align:center;
     }
#box {
     width:790px; 
     height:340px; 
     border:5px solid green;
     margin:50px auto 0;
     position:relative;
 }
a { 
    width:40px; 
    height:40px; 
    background:red;
    filter:alpha(opacity:80); 
    opacity:0.8;
    position:absolute; 
    top:160px; 
    font-size:18px; 
    color:#000; 
    text-align:center; 
    line-height:40px; 
    text-decoration:none;
}
a:hover { 
    filter:alpha(opacity:30); 
    opacity:0.3; 
}
#prev { 
    left:10px; 
}
#next { 
    right:10px; 
}
#p1 { 
    width:790px;
     height:30px; 
     line-height:30px; 
     text-align:center; 
     background:#000; 
     color:#fff; 
     font-size:14px; 
     filter:alpha(opacity:80); 
     opacity:0.8; 
     position:absolute; 
     bottom:0; 
     left:0; 
    }
strong { 
    width:790px; 
    height:30px; 
    line-height:30px; 
    text-align:center; 
    background:#000; 
    color:#fff; 
    font-size:14px; 
    filter:alpha(opacity:80); 
    opacity:0.8; 
    position:absolute; 
    top:0; 
    left:0; 
}
#img1 { 
    width:790px; 
    height:340px; 
}
span { 
    position:absolute; 
    width:400px; 
    height:30px; 
    line-height:30px; 
    text-align:center; 
    top:-50px; 
    left:0; 
    font-family:'微软雅黑'; 
    left:200px;
}
</style>
<script>
window.onload = function (){
	var oPrev = document.getElementById('prev');
	var oNext = document.getElementById('next');
	var oP = document.getElementById('p1');
	var oStrong = document.getElementById('strong1');
	var oImg = document.getElementById('img1');
	var aBtn = document.getElementsByTagName('input');
	
	var arrUrl = [ 'img/01.png', 'img/02.png', 'img/03.png', 'img/04.png' ];
	var arrText = [ '冰箱洗衣机', '超级品牌日', '家电奢品廊', '降温大作战' ];
	var num = 0;
	var onOff = true;
	
	aBtn[0].onclick = function (){ 
        onOff = true; 
        document.getElementsByTagName('span')[0].innerHTML = '图片循环切换'; 
    };
	aBtn[1].onclick = function (){
         onOff = false; 
         document.getElementsByTagName('span')[0].innerHTML = '图片顺序切换'; 
        };
	
	// 初始化
	function fnTab(){
		oP.innerHTML = arrText[num];
		oStrong.innerHTML = num+1 + ' / ' + arrText.length;
		oImg.src = arrUrl[num];
	}
	fnTab();
	
	oPrev.onclick = function (){
		num --;
		if( num == -1 ){
			if(onOff){num = arrText.length-1;}else{
				alert('这已经是第一张了,不能再往前了~~');
				num = 0;
			}
			//num = arrText.length-1;
		}
		fnTab();
	};
	oNext.onclick = function (){
		num ++;
		if( num == arrText.length ){
			if(onOff){num = 0;}else{
				alert('已经到最后一张啦~');
				num = arrText.length-1;
			}
		}
		fnTab();
	};
	
};
</script>
</head>

<body>

<input type="button" value="循环切换" />
<input type="button" value="顺序切换" />


<div id="box"><span>图片切换</span>
	<a id="prev" href="javascript:;"><</a>
	<a id="next" href="javascript:;">></a>
  <p id="p1">图片文字加载中……</p>
  <strong id="strong1">图片数量计算中……</strong>
	<img id="img1" />
</div>

</body>
</html>

效果图:

第三章 web前端开发工程师--JavaScript进阶程序设计 3-3 图片切换综合示例

 第三章 web前端开发工程师--JavaScript进阶程序设计 3-3 图片切换综合示例

相关标签: javascript