纯javaScript实现个性化图片轮播
轮播原理说明<如上图所示>:
1. 画布部分(可视区域)属性说明:overflow:hidden使得超出画布部分隐藏或说不可见。position:relative 会导致自身位置的相对变化,而不会影响其他元素的位置、大小的变化。使得使用了position:absolute 元素相对于画布位置进行定位;
absolute元素脱离了文档结构,产生破坏性,导致父元素坍塌,float元素也会脱离文档结构,absolute元素会悬浮在页面上方,遮挡其他部分显示,这点和PhotoShop图层相似,所以要使用z-index控制出现顺序
2.轮播注意点:左右无限滚动
prev-button 第一张图片的前一张是最后一张图片,
next-button 最后一张图片的下一张图片是第一张,
prev-button、next-button位置的偏移是通过设置prev-img-container、next-img-container的left<相对于画布>属性值
click-select-show-button区域,点击该区域小圆圈是通过上一次图片的所在index,当前点击myIndex, 计算公式:(myIndex-index)*(-图片的宽度width)
3.动画过渡注意点:点击prev-button、next-button、click-select-show-button小圆圈,判定当前是否处于动画状态中
4.定时器setTimeout()、clearTimeout
<实现效果图>
Css样式
/**CSS-style**/
/**画布大小*/
#container {
margin:0 auto;
width: 600px;
height: 400px;
overflow: hidden;/*超出画布部分隐藏*/
position: relative;/*相对定位*/
cursor: pointer;
}
/**图片容器*/
#list {
width: 4200px;
height: 400px;
position: absolute;
z-index:1;
}
#list img { float: left; }
/**轮播选中按钮样式*/
#button {
position: absolute;
bottom: 25px;
left: 175px;
width: 250px;
z-index: 2;
}
#button ul li {
list-style: none;
width: 15px;
border-radius: 50%;
padding: 7.5px;
height: 15px;
margin-right: 10px;
background: green;
float: left;
font:15px/15px "microsoft yahei";
text-align: center;
font-weight: bold;
color: white;
cursor: pointer;
}
#button ul li.chos {
background: orange;
}
#container:hover .arrow{
display: block;
}
#pre {
left: 20px;
}
#next {
right: 20px;
}
/**pre next定位*/
.arrow {
position: absolute;
width: 40px;
height: 40px;
background: black;
z-index: 3;
top: 180px;
text-decoration: none;
text-align: center;
line-height: 40px;
font-size: 40px;
color: white;
opacity: 0.3;
filter: alpha(opacity=0.3);
display: none;
}
/**pre next按钮透明度*/
#container a:hover {
opacity: 0.7;
filter: alpha(opacity=0.7);
}
html代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>纯javaScript实现个性化图片轮播</title> <link rel="stylesheet" type="text/css" href="styles/main.css"> <script type="text/javascript" src="scripts/scroImg.js"></script> </head> <body> <div id="container"> <div id="list" style="left:-600px"> <img src="images/5.jpg"> <img src="images/1.jpg"> <img src="images/2.jpg"> <img src="images/3.jpg"> <img src="images/4.jpg"> <img src="images/5.jpg"> <img src="images/1.jpg"> </div> <div id="button"> <ul> <li index='1'>1</li> <li index='2'>2</li> <li index='3'>3</li> <li index='4'>4</li> <li index='5'>5</li> </ul> </div> <a href="#" class="arrow" id="prev"><</a> <a href="#" class="arrow" id="next">></a> </div> </body> </html>
一、javaScript实现图片轮播
window.onload=function(){
var container=document.getElementById('container');
var list=document.getElementById('list');
var buttons=document.getElementById('button').getElementsByTagName('li');
var prev=document.getElementById('prev');
var next=document.getElementById('next');
var index=1;
var interval=1000;
var timer=null;
var animated=false;
//next
next.onclick=function(){
if (!animated) {
animate(-600);
};
index+=1;
if (index>5) {
index=1;
};
showButton();
console.info('next'+index);
}
//prev
prev.onclick=function(){
if(!animated){
animate(600);
}
index-=1;
if(index<1){
index=5;
}
showButton();
console.info('prev'+index);
}
//animate
function animate(offset){
animated=true;
var left=parseInt(list.style.left)+offset;
var animateTime=600;//位移总时间
var interval=10;//时间间隔
var speed=offset/(animateTime/interval);//每次位移量
var go=function(){//animate内部函数
if ((speed<0 && parseInt(list.style.left)>left) || (speed>0 && parseInt(list.style.left)<left)) {//是否位移
list.style.left=parseInt(list.style.left)+speed+'px';
setTimeout(go,interval)
}else{
list.style.left=left+'px';
if (left<-3000) { //最后一张后面
list.style.left=-600+'px'; //显示前一张
};
if(left>-600){//第一张最前面
list.style.left=-3000+'px';//显示最后一张
}
animated=false;
};
}
go();
}
//chos
function showButton(){
for (var i = 0; i < buttons.length; i++) {
buttons[i].className='';
};
buttons[index-1].className='chos';
}
//buttons-click
for (var i = 0; i < buttons.length; i++) {
buttons[i].onclick=function(){
if(this.className=='chos'){
return;
}
var myIndex=parseInt(this.getAttribute('index'));
var offset=(myIndex-index)*-600; //偏移量
animate(offset);
index=myIndex;//set Index
showButton();
}
};
function play(){
timer=setTimeout(function(){
next.click();
play();
},interval)
}
function stop(){
clearInterval(timer);
}
play();
container.onmouseover=function(){
stop();
}
container.onmouseout=function(){
play();
}
}
二、jQuery实现图片轮播
$(function () {
var container = $('#container');
var list = $('#list');
var buttons = $('#container').find('li');
var prev = $('#pre');
var next = $('#next');
var index = 1;
var len = 5;
var interval = 3000;
var timer;
function animate (offset) {
var left = parseInt(list.css('left')) + offset;
if (offset>0) {
offset = '+=' + offset;
}
else {
offset = '-=' + Math.abs(offset);
}
list.animate({'left': offset}, 300, function () {
if(left > -200){
list.css('left', -600 * len);
}
if(left < (-600 * len)) {
list.css('left', -600);
}
});
}
function showButton() {
buttons.eq(index-1).addClass('chos').siblings().removeClass('chos');
}
function play() {
timer = setTimeout(function () {
next.trigger('click');
play();
}, interval);
}
function stop() {
clearTimeout(timer);
}
next.bind('click', function () {
if (list.is(':animated')) {
return;
}
if (index == 5) {
index = 1;
}
else {
index += 1;
}
animate(-600);
showButton();
});
prev.bind('click', function () {
if (list.is(':animated')) {
return;
}
if (index == 1) {
index = 5;
}
else {
index -= 1;
}
animate(600);
showButton();
});
buttons.each(function () {
$(this).bind('click', function () {
if (list.is(':animated') || $(this).attr('class')=='chos') {
return;
}
var myIndex = parseInt($(this).attr('index'));
var offset = -600 * (myIndex - index);
animate(offset);
index = myIndex;
showButton();
})
});
container.hover(stop, play);
play();
});
源码下载 http://pan.baidu.com/s/1kVfnGF1
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
# js
# jQuery
# 图片轮播
# 原生js和jquery实现图片轮播淡入淡出效果
# 基于jQuery实现淡入淡出效果轮播图
# 原生js和jQuery实现淡入淡出轮播效果
# jQuery实现图片简单轮播功能示例
# jQuery实现的简单图片轮播效果完整示例
# 使用JQuery实现图片轮播效果的实例(推荐)
# jQuery简单自定义图片轮播插件及用法示例
# jQuery实现的图片轮播效果完整示例
# jQuery的图片轮播插件PgwSlideshow使用详解
# jQuery实现图片轮播效果代码(基于jquery.pack.js插件)
# jQuery实现的淡入淡出图片轮播效果示例
# 第一张
# 相对于
# 文档
# 也会
# 上一
# 所示
# 说不
# 图层
# 要使
# 最前面
# 下一张
# 源码下载
# 计算公式
# 使用了
# 偏移量
# 如上图
# ul
# li
# px
# height
相关文章:
香港代理服务器配置指南:高匿IP选择、跨境加速与SEO优化技巧
如何快速搭建高效服务器建站系统?
威客平台建站流程解析:高效搭建教程与设计优化方案
如何用y主机助手快速搭建网站?
黑客入侵网站服务器的常见手法有哪些?
宠物网站制作html代码,有没有专门介绍宠物如何养的网站啊?
网站制作大概要多少钱一个,做一个平台网站大概多少钱?
定制建站价位费用解析与套餐推荐全攻略
香港服务器网站卡顿?如何解决网络延迟与负载问题?
整蛊网站制作软件,手机不停的收到各种网站的验证码短信,是手机病毒还是人为恶搞?有这种手机病毒吗?
上海制作企业网站有哪些,上海有哪些网站可以让企业免费发布招聘信息?
成都响应式网站开发,dw怎么把手机适应页面变成网页?
XML的“混合内容”是什么 怎么用DTD或XSD定义
如何打造高效商业网站?建站目的决定转化率
linux top下的 minerd 木马清除方法
教学论文网站制作软件有哪些,写论文用什么软件
?
网站广告牌制作方法,街上的广告牌,横幅,用PS还是其他软件做的?
如何生成腾讯云建站专用兑换码?
建站之星下载版如何获取与安装?
c++怎么用jemalloc c++替换默认内存分配器【性能】
如何用腾讯建站主机快速创建免费网站?
建站主机与虚拟主机有何区别?如何选择最优方案?
宝塔新建站点为何无法访问?如何排查?
建站之星价格显示格式升级,你的预算足够吗?
建站之星后台密码遗忘如何找回?
c++如何打印函数堆栈信息_c++ backtrace函数与符号名解析【方法】
建站主机选虚拟主机还是云服务器更好?
零服务器AI建站解决方案:快速部署与云端平台低成本实践
成都网站制作公司哪家好,四川省职工服务网是做什么用?
北京企业网站设计制作公司,北京铁路集团官方网站?
青浦网站制作公司有哪些,苹果官网发货地是哪里?
临沂网站制作公司有哪些,临沂第四中学官网?
湖州网站制作公司有哪些,浙江中蓝新能源公司官网?
广州美橙建站如何快速搭建多端合一网站?
建站之星如何开启自定义404页面避免用户流失?
建站之星如何快速生成多端适配网站?
IOS倒计时设置UIButton标题title的抖动问题
极客网站有哪些,DoNews、36氪、爱范儿、虎嗅、雷锋网、极客公园这些互联网媒体网站有什么差异?
如何在宝塔面板中创建新站点?
如何在建站之星绑定自定义域名?
Python lxml的etree和ElementTree有什么区别
如何制作算命网站,怎么注册算命网站?
c++怎么实现高并发下的无锁队列_c++ std::atomic原子变量与CAS操作【详解】
建站168自助建站系统:快速模板定制与SEO优化指南
已有域名和空间,如何快速搭建网站?
佛山网站制作系统,佛山企业变更地址网上办理步骤?
如何通过PHP快速构建高效问答网站功能?
如何在Windows服务器上快速搭建网站?
如何用西部建站助手快速创建专业网站?
较简单的网站制作软件有哪些,手机版网页制作用什么软件?
*请认真填写需求信息,我们会在24小时内与您取得联系。