html中使用JS实现图片轮播效果
1.首先是效果图,要在网页中实现下图的轮播效果,有四张图片,每张图片有自己的标题,然后还有右下角的小方框,鼠标悬浮在小方框上,会切换到对应的图片中去。

2.先是HTML中的内容,最外层是轮播图整个的容器“slideShowContainer”,里边是用来装图片的“picUl”和用来显示小方框的“dotUl”,以及用来装标题的“titleDiv”。
<div id="slideShowContainer">
<ul id="picUl">
<li><a href="#"><img src="img/lunbo1.jpg" alt=""/></a></li>
<li><a href="#"><img src="img/lunbo2.jpg" alt=""/></a></li>
<li><a href="#"><img src="img/lunbo3.jpg" alt=""/></a></li>
<li><a href="#"><img src="img/lunbo4.jpg" alt=""/></a></li>
</ul>
<ul id="dotUl">
<li class="selected">1</li>
<li class="unselected">2</li>
<li class="unselected">3</li>
<li class="unselected">4</li>
</ul>
<div id="titleDiv">
<span class="show"><a href="#">党政机关公务用车有了统一标识</a></span>
<span class="hide"><a href="#">“洛阳创新”亮相第52届巴黎航展</a></span>
<span class="hide"><a href="#">中国河洛乡愁摄影主题公园揭牌</a></span>
<span class="hide"><a href="#">洛阳机场建成生态停车场</a></span>
</div>
</div> 3.然后是css中的样式
#slideShowContainer{
width: 425px;
height: 325px;
margin-top: 10px;
margin-left: 10px;
overflow: hidden;
position: relative;
}
#slideShowContainer img{
width: 425px;
height: 325px;
transition: all 0.6s;
}
#slideShowContainer img:hover{
transform: scale(1.07);
}
#picUl{
list-style: none;
}
#dotUl{
list-style: none;
display: flex;
flex-direction: row;
position: absolute; //使用绝对布局,固定于左下角
right: 21px;
bottom: 15px;
z-index: 2; //通过设置z-index的值大于#titleDiv中z-index的值,使其浮在标题栏的上方
}
#titleDiv{
position: absolute;
width: 100%;
height: 42px;
bottom: 0px;
left: 0px;
background-color: #000000;
opacity:0.6; //设置透明度,实现标题栏半透明效果
z-index: 1;
}
#titleDiv>span{
line-height: 42px;
color: #FFFFFF;
margin-left: 20px;
width: 270px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
#titleDiv>span>a{
color: #FFFFFF;
}
.selected{
width: 12px;
height: 12px;
background-color: #FFFFFF;
color: transparent;
margin-left: 9px;
}
.unselected{
width: 12px;
height: 12px;
background-color: #0069AD;
color: transparent;
margin-left: 9px;
}
.hide{
display: none;
}
.show{
display: block;
}
4.通过js控制,动态修改相应的样式,达到图片轮播的效果
/*图片轮播*/
var slideShowContainer = document.getElementById("slideShowContainer");
var pic = document.getElementById("picUl").getElementsByTagName("li");
var dot = document.getElementById("dotUl").getElementsByTagName("li");
var title = document.getElementById("titleDiv").getElementsByTagName("span");
var index = 0;
var timer = null;
/*定义图片切换函数*/
function changePic (curIndex) {
for(var i = 0;i < pic.length;++i){
pic[i].style.display = "none";
dot[i].className = "unselected";
title[i].className = "hide"
}
pic[curIndex].style.display = "block";
dot[curIndex].className = "selected";
title[curIndex].className = "show";
}
/*index超出图片总量时归零*/
function autoPlay(){
if(+index >= pic.length){
index = 0;
}
changePic(index);
index++;
}
/*定义并调用自动播放函数*/
timer = setInterval(autoPlay,1500);
/*鼠标划过整个容器时停止自动播放*/
slideShowContainer.onmouseover = function(){
clearInterval(timer);
}
/*鼠标离开整个容器时继续播放下一张*/
slideShowContainer.onmouseout = function(){
timer = setInterval(autoPlay,1500);
}
/*遍历所有数字导航实现划过切换至对应的图片*/
for(var i = 0;i < dot.length;i++){
dot[i].onmouseover = function(){
clearInterval(timer);
index = this.innerText-1;
changePic(index)
}
}
html中使用JS实现图片轮播效果的更多相关文章
- 原生js实现图片轮播效果
思路:设置父容器(一定宽度,一定高度,相对定位,子容器超出部分进行隐藏),子容器图片并排(浮动,绝对定位,每次点击进行相应的左或右偏移量) 1.html: <!DOCTYPE html> ...
- 用JS做图片轮播
脚本之家 首页应用手游攻略教程 ﹤首页 >> 网络编程 >> JavaScript >> 网页特效 >> 图象特效 js 图片轮播(5张图片) 作者:m ...
- 原生js实现图片轮播思路分析
一.复习原生js实现图片轮播 1.要点 自动轮播 点击小圆圈按钮,显示相应图片 点击左右箭头,实现向前向后轮播图片 2.实现思路 <div id="container"> ...
- js图片轮播效果实现代码
首先给大家看一看js图片轮播效果,如下图 具体思路: 一.页面加载.获取整个容器.所有放数字索引的li及放图片列表的ul.定义放定时器的变量.存放当前索引的变量index 二.添加定时器,每隔2秒钟i ...
- js实现淘宝首页图片轮播效果
原文:http://ce.sysu.edu.cn/hope2008/Education/ShowArticle.asp?ArticleID=10585 <!DOCTYPE html> &l ...
- CSS3图片轮播效果
原文:CSS3图片轮播效果 在网页中用到图片轮播效果,单纯的隐藏.显示,那再简单不过了,要有动画效果,如果是自己写的话(不用jquery等),可能要费点时间.css3的出现,让动画变得不再是问题,而且 ...
- jQuery个性化图片轮播效果
jQuery个性化图片轮播效果 购物产品展示:图片轮播器<效果如下所示> 思路说明: 每隔一段时间,实现图片的自动切换及选项卡选中效果,鼠标划入图片动画停止,划出或离开动画开始 两个区域: ...
- JQuery简单实现图片轮播效果
很多页面都需要用到界面轮播,但是用原生js相对来说比较复杂,用jQuery实现效果比较迅速,写个简单的demo 1.首先在HTML页面要放置轮播图案位置插入div,这里写了轮播图片数量为3张,所以定义 ...
- JQ 实现轮播图(3D旋转图片轮播效果)
轮播图效果如下: 代码: <!DOCTYPE html> <html xmlns="/www.w3.org/1999/xhtml"> <head> ...
随机推荐
- Why provision Bare Metal
Here are a few use-cases for bare metal (physical server) provisioning in cloud; there are doubtless ...
- 【Java面试题】12 内部类可以引用它的包含类的成员吗?有没有什么限制?
完全可以.如果不是静态内部类,那没有什么限制! 如果你把静态嵌套类当作内部类的一种特例,那在这种情况下不可以访问外部类的普通成员变量,而只能访问外部类中的静态成员,例如,下面的代码: class Ou ...
- jquery-修改、回退结果集
1.end()方法 使用end方法得到上一个结果集 2.addBack()方法 使用addBack()可以得到原结果集与当前结果的合集,也可传入选择器来过滤原结果集
- HEVC有损优化二
1 . HEVC有很些设置对速度的提升有很大的帮助,比如m_bUseEarlyCU,m_useEarlySkipDetection等. 把它们设置成true,会有意想不到的效果. 比如对于不同分辨率 ...
- [转]五分钟看懂UML类图与类的关系详解
在画类图的时候,理清类和类之间的关系是重点.类的关系有泛化(Generalization).实现(Realization).依赖(Dependency)和关联(Association).其中关联又分为 ...
- 如何Request客户端的传值的Data
我们在做B/S的项目,客户端向服务端传值的时候,一般都是request接受. Request常用三个接受方式为:Request.QueryString,Request.Form,Request.Par ...
- JavaScript去空格之trim()
<script> var str=" ab cd "; alert("["+str.trim()+"]"); </scri ...
- windows上SVN图标不显示
症状1:项目左侧导航栏表不能正常显示图标 方法:windows->preferences->General->Appearance->Label Decorations ...
- 在php代码中调用帝国cms头部变量temp.header的方法
在php代码中调用帝国cms头部变量temp.header的方法 代码如下: <?php require("../e/class/connect.php"); if(!def ...
- web开发中的安全问题
web开发中很多东西由前段来负责判断,比如常见的邮箱 电话号码,前端判断到不是一个正确的格式,在你点击提交时候提示你格式填错了,然后不请求后端php,直到你填写正确的格式为止.这种其实可以修改js或者 ...