swiper默认显示三个,中间放大且显示全部图片两边显示部分图片的实现方法
本页面内容最后的红色部分有惊喜哦!
最近在做一个活动页面,要求触摸切换图片时,默认在可视区域中显示三张图片,其中中间的一张图片比其他两张都大且全部显示,而其他两张图片只显示部分即可,于是就想到了swiper这个不依赖于任何js库的插件,但是其官网上没有相应的效果,只有那种可以同时显示三张的demo。但是产品说一定要那种效果,其他的效果不好看,于是我就上网查资料,还真让我找到了解决的办法。如下图:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<title>互融CLUB</title>
<link rel="stylesheet" href="http://m.hurongclub.com/Resource/css/base_new.css">
<link rel="stylesheet" href="http://m.hurongclub.com/Resource/css/swiper.min.css">
<script src="http://m.hurongclub.com/Resource/js/zepto.min.js"></script>
<script>
//计算根节点HTML的字体大小
function htmlFontsize(){
var deviceWidth = document.documentElement.clientWidth;
if(deviceWidth > 750){
deviceWidth = 750;
}
document.documentElement.style.fontSize = deviceWidth / 7.5 + "px";
}
//根节点HTML的字体大小初始化
htmlFontsize();
$(window).resize(function(){
htmlFontsize();
});
</script>
<style>
body{background: #f4664b;font-size:.14rem;}
.swiper-container{height:8.2rem;}
#investproSwiper{margin-top: 0.52rem;}
#investproSwiper .swiper-slide{width:5rem;height:7.25rem;}
#investproSwiper .swiper-slide .investpro{width:4.61rem;height:6.37rem;background-size: 100% 100%;background-repeat: no-repeat;margin-top: 0.58rem;margin-left: 0.19rem;-webkit-transition: all 0.5s linear;}
#investproSwiper .swiper-slide-active .investpro{width:5.16rem;height:7.25rem;margin-left:-.08rem;margin-top:.12rem;}
#investproSwiper .swiper-slide .hrplan_slide{background-image: url(images/hrplan_small.png);}
#investproSwiper .swiper-slide-active .hrplan_slide{background-image: url(images/hrplan_big.png);}
#investproSwiper .swiper-slide .newhand_slide{background-image: url(images/newhand_small.png);}
#investproSwiper .swiper-slide-active .newhand_slide{background-image: url(images/newhand_big.png);}
#investproSwiper .swiper-slide .sxm_slide{background-image: url(images/sxm_small.png);}
#investproSwiper .swiper-slide-active .sxm_slide{background-image: url(images/sxm_big.png);}
.swiper-slide a{width:3.8rem;height: .7rem;background: #eb4e39;display: block;font-size: .36rem;color: #fff;text-align: center;line-height: .7rem;border-radius: .08rem;position: absolute;left: 0;right: 0;margin: auto;bottom: .85rem;z-index: 2;}
.swiper-slide-active .investpro a{width: 4.33rem;height: .8rem;line-height: .8rem;bottom: .4rem;}
.swiper-container-horizontal>.swiper-pagination{height:.5rem;text-align:center;bottom:0;}
.swiper-pagination-bullet{background:#fff;border-radius:50%;width:.12rem;height:.12rem;margin-left:.18rem;opacity:1;}
.swiper-pagination-bullet-active{background:#e04531;}
.investproTab{width:4.6rem;height:6.37rem;position: absolute;top: 0.58rem;z-index: 2;opacity: 0;}
.investpro-prev{left:-3.6rem;}
.investpro-next{right:-3.6rem;}
</style>
</head>
<body>
<div class="swiper-container" id="investproSwiper">
<div class="swiper-wrapper">
<div class="swiper-slide">
<div class="investpro newhand_slide"><a href="##">立即投资</a></div>
</div>
<div class="swiper-slide">
<div class="investpro sxm_slide"><a href="##">立即投资</a></div>
</div>
<div class="swiper-slide">
<div class="investpro hrplan_slide"><a href="##">立即投资</a></div>
</div>
</div>
<div class="swiper-pagination"></div>
<div class="investproTab investpro-prev" id="investpro-prev"></div>
<div class="investproTab investpro-next" id="investpro-next"></div>
</div>
<script src="http://m.hurongclub.com/Resource/js/swiper-3.2.5.min.js"></script>
<script>
var abcSwiper = new Swiper("#investproSwiper", {
slidesPerView: 'auto', //设置slider容器能够同时显示的slides数量
centeredSlides: true, //设定为true时,活动块会居中,而不是默认状态下的居左。
loop: true,
speed: 500,
noSwiping: false, //设置为true时禁止切换
paginationClickable: false,
pagination : '.swiper-pagination',
prevButton:'#investpro-prev',
nextButton:'#investpro-next'
});
</script>
</body>
</html>
以上代码中,最开始的那段js是作为移动端的自适应而写的,不是本文的重点,最后的那段js是swiper官方给出的插件的初始化调用方法,也不多说,最主要的是css代码:
#investproSwiper .swiper-slide{width:5rem;height:7.25rem;}
#investproSwiper .swiper-slide .investpro{width:4.61rem;height:6.37rem;background-size: 100% 100%;background-repeat: no-repeat;margin-top: 0.58rem;margin-left: 0.19rem;-webkit-transition: all 0.5s linear;}
#investproSwiper .swiper-slide-active .investpro{width:5.16rem;height:7.25rem;margin-left:-.08rem;margin-top:.12rem;}
#investproSwiper .swiper-slide .hrplan_slide{background-image: url(images/hrplan_small.png);}
#investproSwiper .swiper-slide-active .hrplan_slide{background-image: url(images/hrplan_big.png);}
#investproSwiper .swiper-slide .newhand_slide{background-image: url(images/newhand_small.png);}
#investproSwiper .swiper-slide-active .newhand_slide{background-image: url(images/newhand_big.png);}
#investproSwiper .swiper-slide .sxm_slide{background-image: url(images/sxm_small.png);}
#investproSwiper .swiper-slide-active .sxm_slide{background-image: url(images/sxm_big.png);}
它的原理就是中间放大且显示全部图片的地方放置的是大图,而其他两张只显示部分图片的地方放置的是该两张图片的小图,待这两张中任何一张图片被切换到中间位置时,显示的就是它的大图了,起主要作用的就是swiper的这个swiper-slide-active
class属性,当当前元素被添加上了这个class后,那么它的子元素出现的就是大图了。不过,你事先要给每张图片都准备一张大图和一张小图。
最后,如果想让两边只显示部分图片的透明度低一点,我们可以这样改写swiper的初始化调用方法:
<script>
var abcSwiper = new Swiper("#investproSwiper", {
slidesPerView: 'auto', //设置slider容器能够同时显示的slides数量
centeredSlides: true, //设定为true时,活动块会居中,而不是默认状态下的居左。
loop: true,
speed: 500,
noSwiping: false, //设置为true时禁止切换
watchSlidesProgress: true, //开启这个参数来计算每个slide的progress(进度、进程)
paginationClickable: false,
pagination : '.swiper-pagination',
prevButton:'#investpro-prev',
nextButton:'#investpro-next',
onProgress: function (a) { var b, c, d; for (b = 0; b < a.slides.length; b++) c = a.slides[b], d = c.progress, scale = 1 - Math.min(Math.abs(.2 * d), 1), es = c.style, es.opacity = 1 - Math.min(Math.abs(d / 2), 1), es.webkitTransform = es.MsTransform = es.msTransform = es.MozTransform = es.OTransform = es.transform = "translate3d(0px,0," + -Math.abs(150 * d) + "px)" },
onSetTransition: function (a, b) { for (var c = 0; c < a.slides.length; c++) es = a.slides[c].style, es.webkitTransitionDuration = es.MsTransitionDuration = es.msTransitionDuration = es.MozTransitionDuration = es.OTransitionDuration = es.transitionDuration = b + "ms" }
});
</script>
如下图所示:
以下附上源代码:
swiper默认显示三个,中间放大且显示全部图片两边显示部分图片
以下更新于2017年7月20日:
上边的那种方法在实现起来有点麻烦,不仅要准备大图,还要准备小图,增加了工作量,最近在项目开发中,突然发现了 swiper 官网上其实早就有这样的案例了,只是我疏于仔细查找,所以才搞的这么麻烦,以下我就给大家贴出swiper实例吧(这样的效果和用手机浏览更配哦!):
3D 覆盖流效果
swiper默认显示三个,中间放大且显示全部图片两边显示部分图片的实现方法的更多相关文章
- swiper轮播问题之二:默认显示3张图片,中间显示全部两边显示部分
其二:项目遇到比较有点要求的轮播图,默认显示3张图片,中间显示全部,两边显示部分.如图: 网上找了也没有找到合适的,最后经过自己摸索写了出来,贴出代码分享给大家. CSS .swipe ...
- java:JQuery(声明,JQ和JS对象的区别,prop,attr,addClass,offset,trigger,dblclick和change事件,hide,show,toggle,slideUp,slideDown,slideToggle,三种选择器,标签的获取,三张图片的放大与缩小)
1.JQuery: jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架).jQuery设计 的宗旨是“ ...
- Lodop背景图无图片时显示放大叉号问题
正常情况下,如果指定图片路径中,图片不存在或指定错误的路径,会因为找不到图片显示叉号,Lodop背景图如果设置了背景图宽度高度控制,显示的叉号也会被相应的放大,形成放大的模糊的图案,看起来就像是黑色边 ...
- Menu显示三个点,不显示内容
先说下menu的使用 首先自定义一个menu选项 <menu xmlns:android="http://schemas.android.com/apk/res/android&quo ...
- echarts使用记录(三):x/y轴数据和刻度显示及坐标中网格显示、格式化x/y轴数据
1.去掉坐标轴刻度线,刻度数据,坐标轴网格,以Y轴为例,同理X轴 xAxis: [{ type: 'category', axisTick: {//决定是否显示坐标刻度 alignWithLabel: ...
- 在HTML中显示图片时希望如果图片不存在或者无法显示时,能够显示默认图片
很多时候,在HTML中显示图片时希望如果图片不存在或者无法显示时,能够显示默认图片.可以通过以下方式: <img src="xxx.jpg" onError="th ...
- css文本超出隐藏 显示三个点
文本超出显示三个点一般分两种情况 一,单行文本超出隐藏 overflow:hidden; text-overflow:ellipsis; white-space:nowrap; 二,多行文本超出隐藏 ...
- zencart设置默认货币三种方法
zencart设置默认货币为USD 方法一:mysql UPDATE configuration SET configuration_value = 'USD' WHERE configuration ...
- HTML横向滚动条和文本超出显示三个小圆点
我们这次要说的就是:现在有很多的公司以及很多的app软件经常使用的两个方法横向滚动条和文本超出三个小圆点 横向滚动条:顾名思义嘛,就是能够一块内容可以横着滑动. 文本超出三个小圆点:文本超出就是当文本 ...
随机推荐
- [转] 最详尽的 JS 原型与原型链终极详解
四. __proto__ JS 在创建对象(不论是普通对象还是函数对象)的时候,都有一个叫做__proto__ 的内置属性,用于指向创建它的构造函数的原型对象. 对象 person1 有一个 __pr ...
- 基于spring security 实现前后端分离项目权限控制
前后端分离的项目,前端有菜单(menu),后端有API(backendApi),一个menu对应的页面有N个API接口来支持,本文介绍如何基于spring security实现前后端的同步权限控制. ...
- Docker docker-compose安装
一.在服务器上敲下面命令即可,将需要的版本号修改一下即可 sudo curl -L "https://github.com/docker/compose/releases/download/ ...
- BZOJ4556 [Tjoi2016&Heoi2016]字符串 SA ST表 二分答案 主席树
原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ4556.html 题目传送门 - BZOJ4556 题意 给定一个长度为 $n$ 的字符串 $s$ . ...
- HDU5692 Snacks DFS序 线段树
去博客园看该题解 题目 HDU5692 Snacks Problem Description 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的 ...
- 065 updateStateByKey的函数API
一:使用场景 1.应用场景 数据的累加 一段时间内的数据的累加 2.说明 每个批次都输出自己批次的数据, 这个时候,可以使用这个API,使得他们之间产生联系. 3.说明2 在累加器的时候,起到的效果和 ...
- 搭建TensorFlow中碰到的一些问题(TensorBoard不是内部或外部指令也不是可运行的程序)~
一.windows10环境+pip python软件包(最新版)+Pycharm软件(过段时间在弄下CUDA和GPU吧) 直接使用pip指令来安装tensorflow软件(如果很久没有更新pip软件包 ...
- HDU 3639 Hawk-and-Chicken (强连通缩点+DFS)
<题目链接> 题目大意: 有一群孩子正在玩老鹰抓小鸡,由于想当老鹰的人不少,孩子们通过投票的方式产生,但是投票有这么一条规则:投票具有传递性,A支持B,B支持C,那么C获得2票(A.B共两 ...
- RESTful架构&简单使用Django rest framework
RESTful架构 1 什么是REST REST全称是Representational State Transfer,中文意思是表述性状态转移. 它首次出现在2000年Roy Fielding的博士论 ...
- Python爬虫:更加优雅的执行JavaScript(PyV8)
https://www.jianshu.com/p/c534d6eb881a?utm_source=oschina-app