当前位置: 首页 > 编程日记 > 正文

滑动轮播图实现最后一张图片无缝衔接第一张图片

原理:使用insertBefore和insertAfter方法调整图片顺序。

测试:firefox/chrome/IE11正常

已知不足:每次播放均使用了一次insertBefore和insertAfter,可考虑在最后一张图的时候将前几张图片整体后移。以后有空再优化。

1、HTML结构

alt的值将作为图片的描述信息出现在infobox里。

<div class="outerbox"><div><a href="http://www.baidu.com"><img src="img/img1.jpg" alt="JavaScript高级程序设计"></a><a href="http://www.w3school.com.cn/"><img src="img/img2.jpg" alt="油藏工程原理与方法"></a><a href="http://www.cnblogs.com/zczhangcui/"><img src="img/img3.jpg" alt="高等数学第六版上册"></a><a href="http://cn.bing.com/"><img src="img/img4.jpg" alt="银河帝国2:基地与帝国"></a><a href="http://cn.bing.com/academic/?FORM=Z9LH2"><img src="img/img5.jpg" alt="三体"></a><a href="http://cn.bing.com/dict/?FORM=HDRSC6"><img src="img/img6.jpg" alt="计算机科学导论"></a></div>    
</div>

2、CSS

为方便轮播组件复用,大部分CSS样式包含在了jq组件中,所以在CSS中只需设置outerbox容器的高度和宽度。

.outerbox{height: 500px;width: 800px;
}

3、jquery轮播插件。

给每个图片设置了data-idx属性来标识它们,使infobox能够与每个图片对应。 

// 定义了DOM对象的slideShow()方法,
// 调用条件:外层容器内部嵌套一个容器,内层容器内放入a标签包裹的img元素,
// 调用方法:$("外层容器").slideShow(形参),可传入0~1个实参,
// 实参说明:一个设定颜色和轮播间隔的对象。形如{color:"#ff7",time:5000},对象可接受0~2个属性。
;(function($){$.fn.extend({"slideShow":function(args){//如果传入一个包含设置参数的对象,那么覆盖默认值var settings=jQuery.extend({color:"#317EF3",time:5000}, args);var i,$outerbox=$(this),$imgs=$outerbox.find('img'),adTimer=null,$innerbox=$outerbox.children('div'),imgnum=$imgs.length,imgwidth=$outerbox.width(),imgheight=$outerbox.height();//给每个图片设置data-idx属性标识它们,使其能够和infobox相对应for(i=0;i<imgnum;i++){$imgs.eq(i).attr('data-idx', i);}//设置各个div的css样式
            $imgs.css({float: 'left',border: 'none'});$outerbox.css({overflow: 'hidden',position: 'relative'});$innerbox.css({width: imgwidth*imgnum+"px",position: 'absolute',left:'0',top:'0'});//在outerbox下新增一个显示alt的divvar infobox=$("<div class='infobox'></div>");$outerbox.append(infobox);var $infobox=$outerbox.children('.infobox');$infobox.css({position: 'absolute',left: '0',bottom:'0',width:imgwidth+10+"px",height:'13%'});var liheight=$infobox.height();var lists="";for(i=0;i<imgnum;i++){lists+="<li><a href=''><span></span></a></li>";}var ullists=$("<ul>"+lists+"</ul>");$infobox.append(ullists);$infobox.find('ul').css({height: liheight+"px",paddingLeft:'0',marginTop:'0',marginBottom:'0'});$infobox.find('li').css({display: 'inline-block',float:'left',marginRight:'3px',background: "rgba(0,0,0,0.4)",height:liheight+"px",width:(imgwidth-(imgnum-1)*3)/imgnum+"px",lineHeight:liheight+'px',verticalAlign:'middle'});$infobox.find('a').css({display: 'inline-block',width:$infobox.find('li').width()+"px",textAlign:'center'});$infobox.find('span').css({display:'inline-block',lineHeight:'1.1em',paddingLeft:liheight*0.2+"px",paddingRight:liheight*0.2+"px",verticalAlign: 'middle',color:'#ddd',fontSize:'12px',wordBreak:'break-all',height:'2.2em',overflow:'hidden'});//获得img上层a的href属性,赋给infobox里的a元素for(i=0;i<imgnum;i++){var link=$innerbox.children('a').eq(i).attr("href");var info=$innerbox.find('img').eq(i).attr("alt");$infobox.find('a').eq(i).attr('href', link);if(info){$infobox.find('span').eq(i).append(info);}else{$infobox.find('span').eq(i).append(i+1);}}//增加左右箭头var arrows=$('<div class="leftarrow arrow">&lt;</div><div class="rightarrow arrow">&gt;</div>');$outerbox.append(arrows);var $arrows=$outerbox.children('.arrow');$arrows.css({width:liheight*0.8+"px",height: liheight*1.5+"px",position:'absolute',top:(imgheight*0.5-liheight*0.75-10)+"px",background: "rgba(0,0,0,0.4)",textAlign:'center',lineHeight:liheight*1.45+'px',fontSize:'1.5em',color:'#ddd',cursor:'pointer'});$outerbox.children('.leftarrow').css('left', '0');$outerbox.children('.rightarrow').css('right', '0');//鼠标放在箭头上时,箭头变色$outerbox.children('.leftarrow').hover(function() {$(this).css('background', settings.color);}, function() {$(this).css('background', 'rgba(0,0,0,0.4)');});$outerbox.children('.rightarrow').hover(function() {$(this).css('background', settings.color);}, function() {$(this).css('background', 'rgba(0,0,0,0.4)');});//点击左右箭头var dataidx;$infobox.find('li').eq(0).css('backgroundColor', settings.color).siblings().css('background', 'rgba(0,0,0,0.4)');$outerbox.on('click', '.arrow', function(event) {if ($(event.target).hasClass('rightarrow')) {if (!$innerbox.is(':animated')) {dataidx=$innerbox.find('a:first').next("a").find('img').attr("data-idx");$infobox.find('li').eq(dataidx).css('backgroundColor', settings.color).siblings().css('background', 'rgba(0,0,0,0.4)');$innerbox.animate({left:-imgwidth}, "normal",function(){$innerbox.find('a:first').insertAfter($innerbox.find('a:last'));$innerbox.css('left', '0');});    }}if ($(event.target).hasClass('leftarrow')) {if (!$innerbox.is(':animated')) {$innerbox.find('a:last').insertBefore($innerbox.find('a:first'));$innerbox.css('left', -imgwidth);$innerbox.animate({left:0}, "normal");dataidx=$innerbox.find('a:first').find('img').attr("data-idx");$infobox.find('li').eq(dataidx).css('backgroundColor', settings.color).siblings().css('background', 'rgba(0,0,0,0.4)');}}});//自动轮播,鼠标放在div上时箭头出现,移走箭头消失$outerbox.hover(function() {$outerbox.find('.leftarrow').stop().animate({left:"0"},300);$outerbox.find('.rightarrow').stop().animate({right:"0"},300);$infobox.stop().animate({bottom:"0"}, 300);if (adTimer) {clearInterval(adTimer);}}, function() {$outerbox.find('.leftarrow').stop().animate({left:-liheight*0.8+"px"},300);$outerbox.find('.rightarrow').stop().animate({right:-liheight*0.8+"px"},300);$infobox.stop().animate({bottom:-(liheight-7)+"px"}, 300);adTimer=setInterval(function () {$outerbox.find('.rightarrow').trigger('click');},settings.time);}).trigger('mouseleave');//鼠标放在下方的颜色块上时移动图片$infobox.find('li').mouseover(function() {var index=$(this).index();var dataidx=$innerbox.find('a:first').find('img').attr("data-idx");$infobox.find('li').eq(index).css('backgroundColor', settings.color).siblings().css('background', 'rgba(0,0,0,0.4)');if(index-dataidx>0){for(i=0;i<Math.abs(index-dataidx);i++){$innerbox.find('a:first').insertAfter($innerbox.find('a:last'));}}else if(index-dataidx<0){for(i=0;i<Math.abs(index-dataidx);i++){$innerbox.find('a:last').insertBefore($innerbox.find('a:first'));}}});return this;}});
})(jQuery);

4、引入jq和该插件,并设置outerbox的宽度和高度,便可以实现滑动循环切换的轮播效果。

$(function(){$(".box").slideShow({color:'red'});
});

转载于:https://www.cnblogs.com/zczhangcui/p/6194351.html

相关文章:

一个完整的Installshield安装程序实例—艾泽拉斯之海洋女神出品(三) --高级设置一...

一个完整的Installshield安装程序实例—艾泽拉斯之海洋女神出品&#xff08;三&#xff09; --高级设置一 原文:一个完整的Installshield安装程序实例—艾泽拉斯之海洋女神出品&#xff08;三&#xff09; --高级设置一上一篇&#xff1a;一个完整的安装程序实例—艾泽拉斯之海…

数据结构,堆和栈和队列的概念

数据结构&#xff0c;堆和栈和队列的概念 1 什么是数据结构 数据结构是计算机存储&#xff0c;组织数据的反复改。数据结构是指相互之间存在的一种或多种特定关系的数据元素集合。 2 数据结构的逻辑结构 1 集合结构&#xff0c;元素都是孤立存在的 2 线性结构 &#xff0c;…

电子白板 矢量 编码_当涉及白板编码采访时,请记住准备

电子白板 矢量 编码by Andy Tiffany通过安迪蒂芙尼(Andy Tiffany) 当涉及白板编码采访时&#xff0c;请记住准备 (When it comes to whiteboard coding interviews, remember to PREP) PREP is a mnemonic I created to help you remember the steps involved in solving whit…

机器学习实战笔记(Python实现)-03-朴素贝叶斯

--------------------------------------------------------------------------------------- 本系列文章为《机器学习实战》学习笔记&#xff0c;内容整理自书本&#xff0c;网络以及自己的理解&#xff0c;如有错误欢迎指正。 源码在Python3.5上测试均通过&#xff0c;代码及…

SQLite.swift的简单使用

使用cocoapod 来进行引入 pod ‘SQLite.swift’ // // SQLiteTool.swift // CreateLectureForSwift // // Created by coder on 2019/6/25. // Copyright © 2019 AlexanderYeah. All rights reserved. // import Foundation import SQLite // id let id Expression(“…

Cypress USB开发文档列表(积累中)

CyUSB.chm(pdf) \Cypress\Cypress Suite USB 3.4.7\Driver Cypress CyUSB.sys Programmers Reference 内容: CyUsb.sys、CyUsb.inf 驱动程序介绍&#xff0c;如何绑定设备到驱动程序&#xff0c;以IOCTL Interface、CYIOCTL.Hd的解释为主要内容的编程指导&#xff0c;主…

对象冒充_使用您的精神探照灯进行冒充冒名顶替综合症

对象冒充by Jaime J. Rios由Jaime J. Rios “Stop that imposter! Seize them!”“停止冒名顶替者&#xff01; 抓住他们&#xff01;” I first spotted my imposter two years ago. It happened when I began learning how to code.两年前&#xff0c;我第一次发现了冒名顶…

grep 函数

linux grep命令1.作用Linux系统中grep命令是一种强大的文本搜索工具&#xff0c;它能使用正则表达式搜索文本&#xff0c;并把匹 配的行打印出来。grep全称是Global Regular Expression Print&#xff0c;表示全局正则表达式版本&#xff0c;它的使用权限是所有用户。 2.格式gr…

iOS weak 自动置为nil的实现

1 weak 自动置为nil的实现 runtime 维护了一个Weak表&#xff0c;weak_table_t 用于存储指向某一个对象的所有Weak指针。Weak表其实是一个哈希表&#xff0c; key是所指对象的地址&#xff0c;value是weak指针的地址的数组。 在对象回收的时候&#xff0c;就会在weak表中进…

iOS 缓存策略

Github https://github.com/gaosboy/kache https://github.com/swtlovewtt/WTRequestCenter https://github.com/hans007/CacheFile Image https://github.com/youger/UIImageView-ASIImageCache转载于:https://www.cnblogs.com/hl666/p/3931182.html

数据结构的简要介绍:图形如何工作

by Michael Olorunnisola通过Michael Olorunnisola 数据结构的简要介绍&#xff1a;图形如何工作 (A Gentle Introduction to Data Structures: How Graphs Work) So who wants to work at Google, Facebook, or maybe LinkedIn? Beyond their grueling interview process, o…

Catel(翻译)-为什么选择Catel

1. 介绍 这篇文章主要是为了说明&#xff0c;我们为什么要使用Catel框架作为开发WPF&#xff0c;Silverlight,和Windows phone7应用程序的开发框架。 2. 通用功能 2.1. 这是你的选择 针对需对开发者&#xff0c;再使用架构的时候是希望有很大的自由度的&#xff0c;但是大部…

iOS 三种类型的Block

Block 的copy 操作 Block 其实来讲有三种类型 全局块 NSConcreteGlobalBlock 栈块 NSConcreteStackBlock 堆块 NSConcreteMallocBlock 全局块存储在全局内存中&#xff0c;相当于单例 栈块存于栈内存中&#xff0c;超出其作用域则马上进行销毁 堆块存在于堆内存中&#x…

2.4G高频PCB天线设计

2.4G高频PCB天线设计转载于:https://www.cnblogs.com/LittleTiger/p/6215262.html

如何一起破解图形化Python调试器

15分钟内从零调试 (Zero-to-Debugging in 15 mins) You don’t realize the value of a debugger until you’re stuck working on a hard-to-visualize problem. But once you fire up a development environment with decent debugging capabilities, you’ll never look bac…

python 之路,Day11 (下)- sqlalchemy ORM

python 之路&#xff0c;Day11 - sqlalchemy ORM 本节内容 ORM介绍sqlalchemy安装sqlalchemy基本使用多外键关联多对多关系表结构设计作业1. ORM介绍 orm英文全称object relational mapping,就是对象映射关系程序&#xff0c;简单来说我们类似python这种面向对象的程序来说一切…

iOS事件响应链

1 如下 NSObject 显然是基类&#xff0c;都是继承与UIResponder. 可以看出UIApplication&#xff0c;UIView&#xff0c;UIViewController都是继承自UIResponder类&#xff0c;可以响应和处理事件 我们都是通过UIResonder 来查找控件的父视图控件。’ 发生触摸事件之后&…

论5级流水32bit risc cpu设计

前段时间用verilog写了一个32bit的risc cpu,五级流水&#xff0c;下板调试已经完全可用&#xff0c;准备后期加入浮点运算器&#xff0c;因为最近事情超级多&#xff0c;因此暂时先把RTL图传上来供大家参考&#xff0c;后面我会讲具体怎么设计。希望大家多多关注 :)转载于:http…

开源项目贡献者_嘿新手开源贡献者:请写博客。

开源项目贡献者by Shubheksha通过Shubheksha 嘿新手开源贡献者&#xff1a;请写博客。 (Hey newbie open source contributors: please blog more.) As a newbie open source contributor, I often felt lost and dejected. I couldn’t figure out how different modules fit…

instanceof, isinstance,isAssignableFrom的区别

instanceof运算符 只被用于对象引用变量&#xff0c;检查左边的被测试对象 是不是 右边类或接口的 实例化。如果被测对象是null值&#xff0c;则测试结果总是false。 形象地&#xff1a;自身实例或子类实例 instanceof 自身类 返回true 例&#xff1a; String snew String(&qu…

POJ - 3538 - Domestic Networks

先上题目&#xff1a; Domestic NetworksTime Limit: 2000MS Memory Limit: 65536KTotal Submissions: 732 Accepted: 204 Special JudgeDescription Alex is a system administrator of Domestic Networks Inc. His network connects apartments and spans over multiple buil…

iOS HitTest 机制

当用户触摸&#xff08;Touch&#xff09;屏幕进行交互时&#xff0c;系统首先要找到响应者&#xff08;Responder&#xff09;。系统检测到手指触摸&#xff08;Touch&#xff09;操作的时候&#xff0c;将Touch 以UIEvent 的方式加入到UIApplication 事件队列中去。UIApplica…

巨石加密_缓解巨石

巨石加密by Ian Belcher伊恩贝尔彻(Ian Belcher) 我们如何将技术堆栈转向基于服务&#xff0c;以开发人员体验为中心的设计 (How we pivoted our tech stack to a service-based, developer experience-focused design) This article documents the problems we experienced w…

Python函数中的参数(一)

函数传递参数时的简要关键点&#xff1a; 1、参数的传递是通过自动将对象赋值给本地变量名来实现的。函数参数在实际中只是Python赋值的一个实例。因为引用是以指针的形式实现的&#xff0c;所有的参数实际上都是通过指针进行传递的。 2、在函数内部的参数名的赋值不会影响调用…

LLDB 调试相关

LLDB 初始 LLDB 是一个有着 REPL 的特性和 C ,Python 插件的开源调试器。LLDB 绑定在 Xcode 内部&#xff0c;存在于主窗口底部的控制台中。调试器允许你在程序运行的特定时暂停它&#xff0c;你可以查看变量的值&#xff0c;执行自定的指令&#xff0c;并且按照你所认为合适的…

javascript优缺点_为什么要在JavaScript中使用静态类型? 优缺点

javascript优缺点by Preethi Kasireddy通过Preethi Kasireddy 为什么要在JavaScript中使用静态类型&#xff1f; 优缺点 (Why use static types in JavaScript? The Advantages and Disadvantages) We covered a lot of ground in Part 1! With syntax out of the way, let’…

大数的减法函数--c语言

代码展示&#xff1a; http://paste.ubuntu.com/23693598/ #include<stdio.h> #include<stdlib.h> #include<string.h> char * largeDiffer(char *a,char *b){ /* 使用说明 传入的a和b只能为整数 结果为a-b;返回的为字符指针&#xff0c;注意数组不要越…

json 基础

json格式 JSON格式&#xff1a;http://www.json.org/ python和JSON的关系请参考&#xff1a;http://docs.python.org/library/json.html JSON建构有两种结构&#xff1a; 1. “名称/值”对的集合&#xff08;A collection of name/value pairs&#xff09;。不同的语言中&#…

iOS 中 load 和 initialize的实现顺序

1 load 函数 调用时机&#xff0c;当类引用进项目的时候执行load函数&#xff0c;在main函数开始之前&#xff0c;与 这个类是否被用到是无关的&#xff0c;每个类的load函数都会自动调用一次。 1 父类和子类都实现load函数的时候&#xff0c;父类的load方法优先于子类 2 类…

需求简报_代码简报:有史以来最怪诞的丑毛衣

需求简报Here are three stories we published this week that are worth your time:这是我们本周发布的三个值得您关注的故事&#xff1a; The geekiest ugly sweater ever: 4 minute read 有史以来最怪异的丑毛衣&#xff1a; 4分钟阅读 Lessons from my post-bootcamp job …