jq自定义插件—$.fn的使用之(轮播图)兼容性都ok

2024-06-04 13:18

本文主要是介绍jq自定义插件—$.fn的使用之(轮播图)兼容性都ok,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>图片轮播</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
<style type="text/css">
html,body{
padding: 0;
margin: 0;
}
ul,ul li{
list-style: none;
margin: 0;
padding: 0;
}
.box{
}

#banner{
position: relative;
height:auto;
overflow: hidden;
}
#banner ul{
position:absolute;
}
#banner ul li{
float: left;
}
#banner ul li img{
width: 100%;
height: 100%;
}
#banner #prevBtn,#banner #nextBtn{
height:80px;
width:30px;
background:rgba(0,0,0,0.5);
position:absolute;
top:50%;
margin-top:-40px;
font-size:30px;
line-height:80px;
text-align:center;
text-decoration:none;
color:white;
opacity: 0;
transition: opacity 0.8s ease;
}
#banner #prevBtn{
left:0;
}
#banner #nextBtn{
right:0;
}
#banner:hover #prevBtn,#banner:hover #nextBtn{
opacity: 1;
}
.dot{
height:10px;
width:10px;
border-radius:10px;
background:#2196f3;
display:inline-block;
margin:5px;
}
.on{
background: #009688;
}
</style>
</head>
<body>
<div class="box">
<div id="banner">
<ul id="banner-wrap">
<li>
<img src="http://www.zxhuan.com/wp-content/uploads/2016/02/img1.jpg">
</li>
<li>
<img src="http://www.zxhuan.com/wp-content/uploads/2016/02/img2.jpg">
</li>
<li>
<img src="http://www.zxhuan.com/wp-content/uploads/2016/02/img3.jpg">
</li>
<li>
<img src="http://www.zxhuan.com/wp-content/uploads/2016/02/img4.jpg">
</li>
<li>
<img src="http://www.zxhuan.com/wp-content/uploads/2016/02/img5.jpg">
</li>
</ul>
</div>
</div>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
(function($){   //形成闭包,将作用域进行限定
$.fn.bannerSwiper=function(option){
this.main={
boxWrap:null,//必填
nextBtn:false,//是否往下启动按钮
prevBtn:false,//是否往上启动按钮
autoPlay:false,//是否启动自动播放
times:3000,//自动轮播的时间间隔,
speed:600,//点击按钮是切换的速度
circle:false,//是否启动小圆点
circleAlign:"center",//小圆点的对其方式
circleClick:false//小圆点是否可以点击
};
var self=this;
this.time=null;
this.options=$.extend({},this.main,option);
self.flag=true;
// 插件入口
this.init=function(){
this.bulid();
}
this.bulid=function(){
var self=this;
var wrap=self.options.boxWrap;
self.num=1;
self.nowTime=+new Date();
self.width=$(window).width();
var firstImg=$(wrap).find('li').first();
var lastImg=$(wrap).find('li').last();
$(wrap).append(firstImg.clone());
$(wrap).prepend(lastImg.clone());
self.length=$(wrap).find('li').length;
$(wrap).width(self.width*self.length);
$(wrap).find('li').width(self.width)
$(wrap).parent().height(480);
$(wrap).parent().width(self.width);
$(wrap).css({'left':-self.width*self.num})
// 是否启动自动轮播
if(self.options.autoPlay){
self.plays();
}
// 是否启动按钮
if(self.options.nextBtn){
self.NextBtn();
}
// 是否启动按钮
if(self.options.prevBtn){
self.prevBtn();
}
// 是否启动小圆点
if(self.options.circle){
self.circle()
}
if(self.options.circleClick){
self.clickCircle();
}


}
// // 鼠标移入时
self.on('mouseenter',function(){
self.stops();
})
// 鼠标移出时
self.on('mouseleave',function(){
self.plays(1);
})


// 开始计时器,自动轮播
this.plays=function(){
var self=this;
// self.stops();
console.log('play')
this.time=setInterval(function(){
self.go(-self.width)
},self.options.times);
}
// 停止计时器
this.stops=function(){
console.log('stop');
clearInterval(self.time)
}
// 手动创建按钮元素
this.prevBtn=function(){
var self=this;
var ele=$("<a href='javascript:;' id='prevBtn'><</a>");
self.append(ele);
$('#prevBtn').bind("click",function(){
self.go(self.width);
})
}
// 手动创建按钮元素
this.NextBtn=function(){
var self=this;
var ele=$("<a href='javascript:;' id='nextBtn'>></a>");
self.append(ele)
$('#nextBtn').bind("click",function(){
self.go(-self.width);
})
}
// 手动创建小圆点
this.circle=function(){
var self=this;
var ele=$('<div id="circle-wrap"></div>');
for(var i=0;i<self.length-2;i++){
$('<a class="dot" href="javascript:;"></a>').appendTo(ele)
}
ele.css({
"position":"absolute",
'bottom':'0',
'right':'0',
'left':'0',
'height':'20px',
"padding":"0 10px",
'text-align':self.options.circleAlign
});
self.append(ele);
self.playCircle(this.num-1);

}
//小圆点指定当前项
this.playCircle=function(num){
$('#circle-wrap').find('.dot').eq(num).addClass('on').siblings().removeClass('on');
}
// 点击小圆点
this.clickCircle=function(){
var self=this;
$('#circle-wrap').find('.dot').on('click',function(){
self.num=$(this).index()+1;
self.circlePlay()
})
}
// 点击小圆点,图片切换
this.circlePlay=function(){
self.flag=true;
if(self.flag){
self.flag=false;
$(self.options.boxWrap).stop().animate({
'left':-self.num*self.width
},self.options.speed,function(){
self.flag=true;
});
}
self.playCircle(this.num-1);
}
// 点击按钮,进行轮播,以及自动轮播
this.go=function(offset){
var self=this;
if(self.flag){
self.flag=false;
if(offset<0){
self.num++;
if(self.num>self.length-2){
self.num=1;
}
}
if(offset>0){
self.num--;
if(self.num<=0){
self.num=self.length-2
}
}
if(Math.ceil($(self.options.boxWrap).position().left)<-(self.length-2)*self.width){
           $(self.options.boxWrap).css({
               'left':-self.width
           });
       }
       if(Math.ceil($(self.options.boxWrap).position().left)>-self.length){
           $(self.options.boxWrap).css({
               'left':-self.width*(self.length-2)
           })
       }
       self.playCircle(this.num-1);
$(self.options.boxWrap).stop().animate({
'left':$(self.options.boxWrap).position().left+offset
},self.options.speed,function(){
self.flag=true;
});
}
}
this.init();
}
})(jQuery);
$('#banner').bannerSwiper({
boxWrap:"#banner-wrap",
nextBtn:true,
prevBtn:true,
autoPlay:true,
circle:true,
circleClick:true
})
</script>
</body>

</html>




/*另外一种最常见的兼容ie7,8的banner*/

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script src="js/jquery.min.js"></script>
<style type="text/css">
#ad {
width: 1350px;
height: 370px;
overflow: hidden;
margin-left: -5px;
position: relative;
}


#ad ul {
list-style: none;
position: absolute;
margin-left: -40px;
}


#ad ul li {
float: left;
width: 1350px;
height: 370px;
position: relative;
}


.slideshortcut a {
color: #000000;
text-decoration: none;
background-color: #fff;
display: block;
position: absolute;
z-index: 500;
top: 150px;
width: 50px;
height: 50px;
border: 1px solid red;
font-size: 40px;
line-height: 40px;
text-align: center;
opacity: 0;
}


.slideshortcut a:hover {
color: #000000;
text-decoration: none;
}


.prev {
left: 150px;
}


.next {
left: 1200px;
}


.jiaodiandiv {
position: absolute;
z-index: 200;
top: 320px;
left: 42%
}


.jiaodiandiv ul {
list-style: none;
}


.jiaodiandiv ul li {
width: 30px;
height: 30px;
margin-left: 10px;
float: left;
border: 1px solid #B7B7B7;
background-color: #B7B7B7;
border-radius: 15px;
text-align: center;
}


#selectli {
background-color: #FF4400;
}


.jiaodiandiv li:hover {
cursor: pointer;
}


.jiaodiandiv span {
font-size: 20px;
line-height: 30px;
}
</style>
 </head>
 <body>
     <div id="ad"> 
       <ul> 
        <li> <a href="#" title="位置1"><img src="ad.png" /></a> </li> 
        <li> <a href="#" title="位置2"><img src="1.jpg" /></a> </li> 
        <li> <a href="#" title="位置3"><img src="2.jpg" /></a> </li> 
        <li> <a href="#" title="位置4"><img src="3.jpg" /></a> </li> 
       </ul> 
      </div> 
      <div class="slideshortcut"> 
       <a id="SlidePrev" class="prev">&lt;</a> 
       <a id="SlideNext" class="next">&gt;</a> 
      </div> 
      <div class="jiaodiandiv"> 
       <ul> 
        <li id="selectli"><span>1</span></li> 
        <li><span>2</span></li> 
        <li><span>3</span></li> 
        <li><span>4</span></li> 
       </ul> 
  </div>
  <script>
  $(document).ready(function() {
    /*轮播*/
    var index = 0;
   // var jdlis = $('.jiaodiandiv li');
    /*焦点li元素集合*/
    var timer;
    var liWidth = $('#ad').width();
    var len = $("#ad ul li").length;
    $("#ad ul").css("width", liWidth * (len));//左右滚动,即所有li元素都是在同一排向左浮动,所以这里需要计算出外围ul元素的宽度
    $("#SlidePrev").click(function() {//上一张按钮
        clearInterval(timer);
        index -= 1;
        if (index == -1) {
            index = len - 1;
        }
        showPic(index);
    });
    $("#SlideNext").click(function() { //下一张按钮
        clearInterval(timer);
        index += 1;
        if (index == len) {
            index = 0;
        }
        showPic(index);
    });
    //轮播
    $('#ad').hover(function() {
        clearInterval(timer);
        /*停止动画*/
        $('.slideshortcut a').show().css('opacity', '0.4');
    },
    function() {
        $('.slideshortcut a').hide();
        timer = setInterval(function() {
            showPic(index);
            index++;
            if (index == len) {
                index = 0;
            }
        },
        2000);
    }).trigger("mouseleave");
    /*显示index图片*/
    function showPic(index) {
        var nowLeft = -index * liWidth;
        jdlis.eq(index).css('backgroundColor', '#FF4400');
        jdlis.not(jdlis.eq(index)).css('backgroundColor', '#B7B7B7');
        $("#ad ul").stop(true, false).animate({
            "left": nowLeft
        },
        300);
    }
    $('.slideshortcut a').mouseover(function() {
        $('.slideshortcut a').show();
    });
    $('.prev').mouseover(function() {
        $(this).css({
            opacity: '0.95',
            cursor: 'pointer'
        });
    });
    $('.next').mouseover(function() {
        $(this).css({
            opacity: '0.95',
            cursor: 'pointer'
        });
    });
    /*点击焦点区的div显示对应图*/
    jdlis.click(function() {
        clearInterval(timer);
        index = jdlis.index(this);
        showPic(index);
    });
});
  </script>
 </body>
 </html>

这篇关于jq自定义插件—$.fn的使用之(轮播图)兼容性都ok的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1030188

相关文章

使用Python实现图像LBP特征提取的操作方法

《使用Python实现图像LBP特征提取的操作方法》LBP特征叫做局部二值模式,常用于纹理特征提取,并在纹理分类中具有较强的区分能力,本文给大家介绍了如何使用Python实现图像LBP特征提取的操作方... 目录一、LBP特征介绍二、LBP特征描述三、一些改进版本的LBP1.圆形LBP算子2.旋转不变的LB

Maven的使用和配置国内源的保姆级教程

《Maven的使用和配置国内源的保姆级教程》Maven是⼀个项目管理工具,基于POM(ProjectObjectModel,项目对象模型)的概念,Maven可以通过一小段描述信息来管理项目的构建,报告... 目录1. 什么是Maven?2.创建⼀个Maven项目3.Maven 核心功能4.使用Maven H

Python中__init__方法使用的深度解析

《Python中__init__方法使用的深度解析》在Python的面向对象编程(OOP)体系中,__init__方法如同建造房屋时的奠基仪式——它定义了对象诞生时的初始状态,下面我们就来深入了解下_... 目录一、__init__的基因图谱二、初始化过程的魔法时刻继承链中的初始化顺序self参数的奥秘默认

SpringBoot使用GZIP压缩反回数据问题

《SpringBoot使用GZIP压缩反回数据问题》:本文主要介绍SpringBoot使用GZIP压缩反回数据问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot使用GZIP压缩反回数据1、初识gzip2、gzip是什么,可以干什么?3、Spr

Spring Boot 集成 Quartz并使用Cron 表达式实现定时任务

《SpringBoot集成Quartz并使用Cron表达式实现定时任务》本篇文章介绍了如何在SpringBoot中集成Quartz进行定时任务调度,并通过Cron表达式控制任务... 目录前言1. 添加 Quartz 依赖2. 创建 Quartz 任务3. 配置 Quartz 任务调度4. 启动 Sprin

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

Java使用SLF4J记录不同级别日志的示例详解

《Java使用SLF4J记录不同级别日志的示例详解》SLF4J是一个简单的日志门面,它允许在运行时选择不同的日志实现,这篇文章主要为大家详细介绍了如何使用SLF4J记录不同级别日志,感兴趣的可以了解下... 目录一、SLF4J简介二、添加依赖三、配置Logback四、记录不同级别的日志五、总结一、SLF4J

使用Python实现一个优雅的异步定时器

《使用Python实现一个优雅的异步定时器》在Python中实现定时器功能是一个常见需求,尤其是在需要周期性执行任务的场景下,本文给大家介绍了基于asyncio和threading模块,可扩展的异步定... 目录需求背景代码1. 单例事件循环的实现2. 事件循环的运行与关闭3. 定时器核心逻辑4. 启动与停

如何使用Nginx配置将80端口重定向到443端口

《如何使用Nginx配置将80端口重定向到443端口》这篇文章主要为大家详细介绍了如何将Nginx配置为将HTTP(80端口)请求重定向到HTTPS(443端口),文中的示例代码讲解详细,有需要的小伙... 目录1. 创建或编辑Nginx配置文件2. 配置HTTP重定向到HTTPS3. 配置HTTPS服务器

Java使用ANTLR4对Lua脚本语法校验详解

《Java使用ANTLR4对Lua脚本语法校验详解》ANTLR是一个强大的解析器生成器,用于读取、处理、执行或翻译结构化文本或二进制文件,下面就跟随小编一起看看Java如何使用ANTLR4对Lua脚本... 目录什么是ANTLR?第一个例子ANTLR4 的工作流程Lua脚本语法校验准备一个Lua Gramm