jquery实现走马灯特效(扑克牌切换效果)

本文主要是介绍jquery实现走马灯特效(扑克牌切换效果),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

话不多说,先上大致效果:

在这里插入图片描述

本着程序员的开源精神,代码奉上:

html代码:

<div class="main-box"><div class="poker_box"><div class="pokerCaroursel poker-content" data-setting='{"width":1094,"height":338,"pokerWidth":488,"pokerHeight":338,"scale":0.85,"speed": 500,"autoPlay":true,"delay":2000,"verticalAlign":"middle"}'><!-- 向左按钮 --><div class="poker-btn poker-prev-btn"></div><ul class="poker-list"><!-- 图片张数最好是基数,这样呈现的画面才好看 --><li class="poker-item poker-rad"><div class="item"><a href="#"><img class="tabImg" src="images/01.jpg"></a> <span class="poker-item-title">图一</span></div></li><li class="poker-item poker-rad"><div class="item"><a href="#"><img class="tabImg" src="images/02.jpg"></a><span class="poker-item-title">图二</span> </div></li><li class="poker-item poker-rad"><div class="item"><a href="#"><img class="tabImg" src="images/03.jpg"></a><span class="poker-item-title">图三</span></div></li></ul><!-- 向右按钮 --><div class="poker-btn poker-next-btn"></div></div></div></div>

注意:

  1. 类名不要随意更改(否则插件里面的也要更改),替换图片地址即可。
  2. data-setting属性记得按照以上格式设置,不然可能报错

script代码:

<script src="jquery.js"></script>
<script src="jquery.pokerCarousel.js"></script>
<script>
pokerCaroursel.init($('.pokerCaroursel'))
</script>	

注意:
要记得先引入jquery.js文件(可去官网下载:https://jquery.com/download/),再引入jquery.pokerCarousel.js文件

jquery.pokerCarousel.js文件完整代码:

var MIDDLE_PIC_POS = 1//计算如何用最短的距离移动到目标
//由于有两种移动方式,向左边移动或者像右边移动,只需要在这两种方式中选择一个小的就行了;(function($){var pokerCaroursel = function (caroursel){var self = this;this.caroursel = caroursel;this.pokerList = caroursel.find(".poker-list");this.pokerItems = caroursel.find(".poker-item");this.firstpokerItem = this.pokerItems.first();this.lastpokerItem = this.pokerItems.last();this.prevBtn = this.caroursel.find(".poker-prev-btn");this.nextBtn = this.caroursel.find(".poker-next-btn");this.buttonItems = caroursel.find(".tabBtn");//每个移动元素的位置索引,用于记录每个元素当前的位置,在每次移动的时候,该数组的值都会发生变化//数组的下标对应li元素的位置索引this.curPositions = [];for(var i = 0;i<this.pokerItems.length;++i){this.curPositions[i] = i+1;}this.setting = {"width":"1106","height":"340","pokerWidth":"488","pokerHeight":"340","scale":"0.8","speed":"1000","isAutoplay":"true","dealy":"2000"};$.extend(this.setting,this.getSetting());this.setFirstPosition();this.setSlicePosition();this.refreshCss();this.rotateFlag = true;this.prevBtn.bind("click",function(){if(self.rotateFlag){self.rotateFlag = false;self.rotateAnimate("left")}});this.nextBtn.bind("click",function(){if(self.rotateFlag){self.rotateFlag = false;self.rotateAnimate("right")}});//绑定位置按钮事件this.buttonItems.each(function(index){var _this = $(this);_this.click(function(){self.clickPosButtonIndex(index);})});if(this.setting.isAutoplay){this.autoPlay();this.caroursel.hover(function(){clearInterval(self.timer)},function(){self.autoPlay()})}};pokerCaroursel.prototype = {autoPlay:function(){var that = this;this.timer =  window.setInterval(function(){that.nextBtn.click();},that.setting.dealy)},refreshCss:function(){var that= this;var curFirstPos;//当前位于中间的li元素位置this.buttonItems.each(function(index){var _this = $(this);var curPos = that.curPositions[index];if(curPos == 1){_this.addClass('poker-btn-active');}else{_this.removeClass('poker-btn-active');}});},//记录每次移动的状态refreshPositions:function(offset){for(var i = 0; i < this.curPositions.length; ++i){var nextPos = this.curPositions[i] + offset;if (nextPos > this.curPositions.length) {//移动超过末尾,则位置变成到开头nextPos = nextPos - this.curPositions.length;}else if (nextPos < 1) {向左边移动已经移动到开始位置更左边,则位置变成结束nextPos = this.curPositions.length + nextPos;}this.curPositions[i] = nextPos;}//console.log('after refreshPositions',this.curPositions);this.refreshCss();},cal_move_path:function(curPos,desPos,arraySize) {if(curPos == desPos) return null;//往左边移动var goRightSteps;var goLeftSteps;var retDirect;var retStep;if(curPos > desPos){goRightSteps = curPos - desPos;goLeftSteps = desPos + (arraySize - curPos);retDirect = (goRightSteps <= goLeftSteps) ? "left":"right";return {"direct":retDirect,"step":Math.min(goLeftSteps,goRightSteps)};}},//点击位置按钮,根据点击的按钮索引 决定向左还是向右移动[因为只有三个位置,该方法能够仅靠向左或向右就能将//指定的位置移动到中间]clickPosButtonIndex:function(index){var self = this;if(self.rotateFlag == false) {//目前正在移动等移动结束后才能进入return;}var curPos = this.curPositions[index];var retPath = this.cal_move_path(curPos,MIDDLE_PIC_POS,this.curPositions.length);if (retPath == null){return;}var direct = retPath.direct;var step = retPath.step;self.rotateFlag = false;self.rotateAnimate(direct,step)},rotateAnimate:function(type,step){step = step || 1;var that = this;var zIndexArr = [];var speed = that.setting.speed;this.pokerItems.each(function(){var self = $(this);var destPic = null;var curPic = self;for (var i = 0; i < step;++i){if(type == "left"){// 向左边移动, 下一张图片在自己的右边,所以用next()destPic = curPic.next().get(0)?curPic.next():that.firstpokerItem;}else{destPic = curPic.prev().get(0)?curPic.prev():that.lastpokerItem;}curPic = destPic;}var width = destPic.css("width");var height = destPic.css("height");var zIndex = destPic.css("zIndex");var opacity = destPic.css("opacity");var left = destPic.css("left");var top = destPic.css("top");zIndexArr.push(zIndex);self.animate({"width":width,"height":height,"left":left,"opacity":opacity,"top":top},speed,function(){that.rotateFlag = true;});});this.pokerItems.each(function(i){$(this).css("zIndex",zIndexArr[i]);});if (type == 'right'){this.refreshPositions(-step);}else{this.refreshPositions(step);}},setFirstPosition:function(){this.caroursel.css({"width":this.setting.width,"height":this.setting.height});this.pokerList.css({"width":this.setting.width,"height":this.setting.height});var width = (this.setting.width - this.setting.pokerWidth) / 2;console.log(this.pokerItems.length)this.prevBtn.css({"width":width , "height":this.setting.height,"zIndex":Math.ceil(this.pokerItems.length/2)});this.nextBtn.css({"width":width , "height":this.setting.height,"zIndex":Math.ceil(this.pokerItems.length/2)});this.firstpokerItem.css({"width":this.setting.pokerWidth,"height":this.setting.pokerHeight,"left":width,"zIndex":Math.ceil(this.pokerItems.length/2),"top":this.setVertialType(this.setting.pokerHeight)});},setSlicePosition:function(){var _self = this;var sliceItems = this.pokerItems.slice(1),level = Math.floor(this.pokerItems.length/2),leftItems = sliceItems.slice(0,level),rightItems = sliceItems.slice(level),pokerWidth = this.setting.pokerWidth,pokerHeight = this.setting.pokerHeight,Btnwidth = (this.setting.width - this.setting.pokerWidth) / 2,gap = Btnwidth/level,containerWidth = this.setting.width;var i = 1;var leftWidth = pokerWidth;var leftHeight = pokerHeight;var zLoop1 = level;leftItems.each(function(index,item){var scale = _self.setting.scale;if(index==1){scale = scale*scale;}leftWidth = pokerWidth * scale;leftHeight = pokerHeight*scale;$(this).css({"width":leftWidth,"height":leftHeight,"left": Btnwidth - i*gap,"zIndex":zLoop1--,"opacity":2/(i+1),"top":_self.setVertialType(leftHeight)});i++;});var j = level;var zLoop2 = 1;var rightWidth = pokerWidth;var rightHeight = pokerHeight;rightItems.each(function(index,item){var scale = _self.setting.scale;if(index==0){scale = scale*scale;}var rightWidth = pokerWidth * scale;var rightHeight = pokerHeight*scale;$(this).css({"width":rightWidth,"height":rightHeight,"left": containerWidth -( Btnwidth - j*gap + rightWidth),"zIndex":zLoop2++,"opacity":2/(j+1),"top":_self.setVertialType(rightHeight)});j--;});},getSetting:function(){var settting = this.caroursel.attr("data-setting");if(settting.length > 0){return $.parseJSON(settting);}else{return {};}},setVertialType:function(height){var align = this.setting.align;if(align == "top") {return 0}else if(align == "middle"){return (this.setting.pokerHeight - height) / 2}else if(align == "bottom"){return this.setting.pokerHeight - height}else {return (this.setting.pokerHeight - height) / 2}}};pokerCaroursel.init = function (caroursels){caroursels.each(function(index,item){new pokerCaroursel($(this));})  ;};window["pokerCaroursel"] = pokerCaroursel;
})(jQuery);// JavaScript Document

css代码:

.main-box{ height: 352px; width:1118px;position: absolute;top: 122px;left: 32px;}
.poker_box h2 {font-size: 30px;color: #015293;font-weight: bold;text-align: center;}
.poker_box h3 {font-size: 16px;color: #015293;margin: 10px 0 35px;text-align: center;}
.poker-content {width: 1129px;position: relative;width: 100%;height: 350px!important;margin-left: auto;margin-right: auto;}
.poker-content img {display: block;box-shadow: 0px 0px 10px #222222;-webkit-box-shadow: 0px 0px 10px #222222;border: 0;}.poker-content a, .poker-content img {display: block;width: 100%;height: 100%;border: none;}
img {border: none;display: block;}
.poker-content .poker-list {width: 1118px;height: 500px;}
.poker-content .poker-list .poker-item {width: 200px;height: 350px;position: absolute;left: 0;top: 0;}
.poker-rad{  border-radius: 20px;  overflow: hidden;}
.poker-rai{  border-radius: 20px;  overflow: hidden;}
.poker-content .poker-list .poker-item .item {position: relative;width: 100%;height: 100%;}
.poker-content .poker-btn {position: absolute;top: 0;cursor: pointer;filter: opacity(.5) brightness(1);}
.poker-content .poker-btn:hover {filter: opacity(1) brightness(2);}
.poker-content .poker-prev-btn {left: 0;}
.poker-content .poker-next-btn {right: 0;}
.poker-item-title {background:rgba(42, 42, 42, 0.8) none repeat scroll 0 0 !important;/*实现FF背景透明,文字不透明*/filter:Alpha(opacity=80); background:#2a2a2a;text-align: center;color: #FFF;width: 100%;height: 52px;line-height: 52px;position: absolute;bottom: 0;text-indent: 29px}

这篇关于jquery实现走马灯特效(扑克牌切换效果)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

使用Java解析JSON数据并提取特定字段的实现步骤(以提取mailNo为例)

《使用Java解析JSON数据并提取特定字段的实现步骤(以提取mailNo为例)》在现代软件开发中,处理JSON数据是一项非常常见的任务,无论是从API接口获取数据,还是将数据存储为JSON格式,解析... 目录1. 背景介绍1.1 jsON简介1.2 实际案例2. 准备工作2.1 环境搭建2.1.1 添加

Java实现任务管理器性能网络监控数据的方法详解

《Java实现任务管理器性能网络监控数据的方法详解》在现代操作系统中,任务管理器是一个非常重要的工具,用于监控和管理计算机的运行状态,包括CPU使用率、内存占用等,对于开发者和系统管理员来说,了解这些... 目录引言一、背景知识二、准备工作1. Maven依赖2. Gradle依赖三、代码实现四、代码详解五

java如何分布式锁实现和选型

《java如何分布式锁实现和选型》文章介绍了分布式锁的重要性以及在分布式系统中常见的问题和需求,它详细阐述了如何使用分布式锁来确保数据的一致性和系统的高可用性,文章还提供了基于数据库、Redis和Zo... 目录引言:分布式锁的重要性与分布式系统中的常见问题和需求分布式锁的重要性分布式系统中常见的问题和需求

SpringBoot基于MyBatis-Plus实现Lambda Query查询的示例代码

《SpringBoot基于MyBatis-Plus实现LambdaQuery查询的示例代码》MyBatis-Plus是MyBatis的增强工具,简化了数据库操作,并提高了开发效率,它提供了多种查询方... 目录引言基础环境配置依赖配置(Maven)application.yml 配置表结构设计demo_st

python使用watchdog实现文件资源监控

《python使用watchdog实现文件资源监控》watchdog支持跨平台文件资源监控,可以检测指定文件夹下文件及文件夹变动,下面我们来看看Python如何使用watchdog实现文件资源监控吧... python文件监控库watchdogs简介随着Python在各种应用领域中的广泛使用,其生态环境也

el-select下拉选择缓存的实现

《el-select下拉选择缓存的实现》本文主要介绍了在使用el-select实现下拉选择缓存时遇到的问题及解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录项目场景:问题描述解决方案:项目场景:从左侧列表中选取字段填入右侧下拉多选框,用户可以对右侧

Python pyinstaller实现图形化打包工具

《Pythonpyinstaller实现图形化打包工具》:本文主要介绍一个使用PythonPYQT5制作的关于pyinstaller打包工具,代替传统的cmd黑窗口模式打包页面,实现更快捷方便的... 目录1.简介2.运行效果3.相关源码1.简介一个使用python PYQT5制作的关于pyinstall

使用Python实现大文件切片上传及断点续传的方法

《使用Python实现大文件切片上传及断点续传的方法》本文介绍了使用Python实现大文件切片上传及断点续传的方法,包括功能模块划分(获取上传文件接口状态、临时文件夹状态信息、切片上传、切片合并)、整... 目录概要整体架构流程技术细节获取上传文件状态接口获取临时文件夹状态信息接口切片上传功能文件合并功能小

python实现自动登录12306自动抢票功能

《python实现自动登录12306自动抢票功能》随着互联网技术的发展,越来越多的人选择通过网络平台购票,特别是在中国,12306作为官方火车票预订平台,承担了巨大的访问量,对于热门线路或者节假日出行... 目录一、遇到的问题?二、改进三、进阶–展望总结一、遇到的问题?1.url-正确的表头:就是首先ur

C#实现文件读写到SQLite数据库

《C#实现文件读写到SQLite数据库》这篇文章主要为大家详细介绍了使用C#将文件读写到SQLite数据库的几种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下... 目录1. 使用 BLOB 存储文件2. 存储文件路径3. 分块存储文件《文件读写到SQLite数据库China编程的方法》博客中,介绍了文