javascript之轮播图效果
来源:互联网 发布:php 判断大小 编辑:程序博客网 时间:2024/06/06 20:15
html部分代码如下:
<div class="banner">
<div id="adver" class="adver" style="left:0">
<img src="images/b1.jpg" alt="" width="1226px" height="460px">
<img src="images/b2.jpg" alt="" width="1226px" height="460px">
<img src="images/b3.jpg" alt="" width="1226px" height="460px">
<img src="images/b4.jpg" alt="" width="1226px" height="460px">
</div>
<div id="arrows" class="arrows">
<span id="prev"><</span>
<span id="next">></span>
</div>
<div id="buttons" class="buttons">
<span class="selected">1</span>
<span>2</span>
<span>3</span>
<span>4</span>
</div>
</div>
css部分代码如下:
body,span {
margin: 0;
padding: 0;
}
div.banner {
position: relative;
width: 1226px;
height: 460px;
margin: 0 auto;
overflow: hidden;
}
/*轮播图样式
设置容器,使所有图片在在容器内横向排列。隐藏超出部分。
父元素定位方式为相对定位,子元素均为绝对定位,并设置z-index,让底部按钮与左右按钮显示在图片之上。
通过更改容器的left值来实现轮播图效果。
*/
div.adver{
position: absolute;
z-index: 1;
width: 400%;
height: 100%;
}
div.adver img {
float: left;
}
/*左右样式*/
div.arrows {
position: absolute;
top: 200px;
width: 100%;
z-index: 3;
}
.arrows span {
width: 30px;
border-radius: 10%;
line-height: 50px;
background: #ddd;
text-align: center;
}
.arrows span:hover{
background: #eee;
}
.arrows span:nth-child(1) {
float: left;
}
.arrows span:nth-child(2) {
float: right;
}
/*底部按钮样式*/
div.buttons {
position: absolute;
bottom: 20px;
left: 590px;
z-index: 4;
}
.buttons span {
display: inline-block;
width: 20px;
height: 20px;
border: 1px solid black;
border-radius: 50%;
background: #ccc;
text-align: center;
}
.buttons span:hover,.buttons .selected{
background: white;
}
完成css和html之后的显示效果如下图:
接下来,使用JS实现轮播图效果。
// 获取元素
var banner=document.getElementById("banner");
var adver=document.getElementById("adver");
var prev=document.getElementById("prev");
var next=document.getElementById("next");
var buttons=document.getElementById("buttons").getElementsByTagName("span");
var w=parseInt(getStyle(banner,'width'));
var timer=null;
var index=0; //存储当前图片状态0-3
// 首先,实现点击左右两个按钮切换图片效果,使用ele.style获取元素css样式时,如果在js代码中没有先写入ele.style样式时,是读取不到数据的。使用该方法,想要读取到数据,只能将style设置为内联样式。
// 想要获取内部与外部的css样式,可以使用currentStyle和getComputedStyle方法,以上两种方法只能读取,不能写入。
function getStyle(obj,name){
if(obj.currentStyle){
return obj.currentStyle[name];
}else{
return getComputedStyle(obj,false)[name];
}
}
// 定义切换图片函数 获取原来left值 点击一次之后的left值为newleft 判断newleft值 实现最后一张图到第一张,与第一张到第四张的切换
function animate(n){
var left=parseInt(adver.style.left);
var newLeft=left+n;
if(newLeft<-w*3){
newLeft=0;
}
if (newLeft>0) {
newLeft=-w*3;
};
adver.style.left=newLeft+"px";
}
// 给左右两个元素绑定onclick事件
prev.onclick=function(){
index--;
index<0&&(index=3);
animate(w);
buttonShow();
}
next.onclick=function(){
index++;
index>3&&(index=0);
animate(-w);
buttonShow();
}
// 利用定时器实现图片自动切换效果,在鼠标移入显示区域时,动画停止,移出时开始。
function showAuto(){
timer=setInterval(function(){
next.onclick()
},1500)
}
showAuto();
banner.onmouseover=function(){
clearInterval(timer);
timer=null;
}
banner.onmouseout=showAuto;
// 图片移动时底部按钮样式改变。
function buttonShow(){
for(var i=0;i<buttons.length;i++){
if(buttons[i].className='selected'){
buttons[i].className='';
}
buttons[index].className='selected';
}
}
//实现点击底部按钮切换图片效果,同样是利用left便宜了来解决,首先,给buttons下的每个span绑定鼠标点击事件。利用for循环绑定事件时,通过测试会发现,输出的i值永远是最后一个i值。这是因为在for循环时,函数并没有执行。在执行函数是,for循环结束,所有获取到的是最后一个i值。让函数直接执行-函数自调.
for(var i=0;i<buttons.length;i++){
(function(i){
buttons[i].onclick=function(){
animate(w*(index-i));
index=i;//更改index为当前图片值
buttonShow();//改变底部样式
}
})(i)
}
- javascript之轮播图效果
- javascript之广告效果
- 原生Javascript之拖曳效果
- javascript轮播图,幻灯片效果
- javascript 轮播图效果实现
- javascript实现轮播图效果
- JavaScript实现轮播图效果
- JavaScript经典效果集锦之二
- JavaScript经典效果集锦之三
- JavaScript经典效果集锦之四,五
- JavaScript小练习效果之多级菜单
- Javascript实践之时间显示效果
- Javascript 效果
- javascript 效果
- JavaScript学习笔记2之特殊公告栏效果
- JavaScript效果备忘录之输入后显示提示
- JAVASCRIPT之弹出层、弹窗效果+拖曳功能
- javascript dom编程艺术学习笔记之实现动画效果
- JVM运行时数据区
- 金海佳学C++primer 练习9.41
- 在测量和校准应用的焦点的XCP
- 去除List集合中的重复值(四种好用的方法)
- 常见排序算法详解 (收藏!)
- javascript之轮播图效果
- php查询数据库后在页面上显示中文乱码的问题
- 【每日一题(15)】超级楼梯 HDU
- java 查找本机ip地址
- centos安装mongodb
- PAT 1005 The Largest Generation (25)
- SpringBoot集成MyBatis时要配置的Mapper中的xml
- ug472--xilinx 时钟资源学习总结(1)
- 基于QT实现的简易3*3拼图游戏