使用jq和css3自制横向左右滑动图片列表

2024-05-30 02:58

本文主要是介绍使用jq和css3自制横向左右滑动图片列表,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

展示如下
这里写图片描述

目标效果

  1. 默认选中第一张,鼠标悬浮缓慢放大图片,底部文字介绍由下往上缓慢滑出,选中后边框效果;
  2. 左右滑动,距离为四张图片。

思路

  1. 定义一个定宽的容器,然后图片不换行并排显示,超出部分隐藏,隐藏x轴滚动条;
  2. 左右按钮点击,获取容器的滚动条位置,在此基础上加上固定移动距离,然后重置容器滚动条位置。

做法

  • 左右箭头可以使用图片,我这里使用的是:before和:after伪类做的,通过倾斜加定位凑成一个箭头,这样做箭头前端是平的,不是尖的。还有一种做法是用height:0;width:0;然后通过边框做的,这种做法需要把背景色设置和页面颜色一样,看起来是个中空的箭头

  • 图片并排显示需要给容器使用 white-space: nowrap,然后容器定宽高,并且overflow:hidden;

  • 鼠标悬浮放大:很简单,使用transition和transform做

.box{width: 50px;height: 50px;background-color: red;transition: all 0.6s;cursor: pointer;
}.box:hover{transform: scale(1.1);
}
  • 底部文字缓慢从下往上滑出也是使用transition和transform做
/*info应该被包裹在box里*/
.box{position: relative;
}.box .info{position: absolute;left: 0;bottom: 0;transition: height 0.6s;width: 100%;height: 0;
}.box:hover > .info{height: auto;
}
/*仅做示范,实际可以看本文最后代码*/
  • 选中后保持放大,并且加亮边框,可以直接将它的样式换成放大后的就可以
/*js*/
//jq对attr方法渐渐的使用prop去取代,有时使用attr方法获取不到你要的属性
$(".box").click(function(){$(this).prop('class','box-active');
});/*css*/
//有时直接计算放大后的宽高得不到整数,直接设置宽高,点击效果会像移动了,可以直接使用transform进行原比例放大.box-active{width: 50px;height: 50px;transform: scale(1.1);background-color: red;cursor: pointer;border: 2px solid #0068b7;
}

这样做出来的效果依然会出现点击后,图片像移动了一样,是因为多出了2px的边框宽度导致,可以给box加一个和背景一样颜色的2px边框,选中后将颜色换成亮色,由于边框一直存在,切换颜色就不会导致位移; 在有阴影时颜色设为和阴影同色。

  • 点击左右按钮图片移动
/*js*/$(".turn-right").click(function(){var obj = $(".box");if(obj.scrollWidth - obj.scrollLeft() < 1177){obj.animate({scrollLeft: obj.scrollWidth},1000);}else{obj.animate({scrollLeft:obj.scrollLeft()+1177},1000);}
});$(".turn-left").click(function(){var obj = $(".box");if(obj.scrollLeft() < 1177){obj.animate({scrollLeft:0},1000);}else{obj.animate({scrollLeft:obj.scrollLeft()-1177},1000);}
});

obj.scrollWidth: 获取滚动条宽度(除滚动条本身按钮的宽度)

obj.animate: 设置滑动效果。scrollLeft:滚动至距离滚动条多少的位置。1000动画执行时间ms

1177:移动固定距离,可以根据自己需求定制


以下为所有代码,仅供参考

html

<div class="match container"><div class="content"><div class="slide"><div class="turn-left"><span class="arrow-left"></span></div><ul class="match-list"><li class="match-item"><img src="1.png" alt="" width="100%" /><div class="info"><p>球场:<span class="color-blue">成都观岭</span></p><p>一杆进洞奖金:<span class="color-yellow">2000元</span></p><p>2017.05.01~2017.05.31</p></div></li><li class="match-item"><img src="1.png" alt="" width="100%" /><div class="info"><p>球场:<span class="color-blue">成都观岭</span></p><p>一杆进洞奖金:<span class="color-yellow">2000元</span></p><p>2017.05.01~2017.05.31</p></div></li><li class="match-item"><img src="2.png" alt="" width="100%" /></li><li class="match-item"><img src="2.png" alt="" width="100%" /></li><li class="match-item"><img src="3.png" alt="" width="100%" /></li><li class="match-item"><img src="3.png" alt="" width="100%" /></li><li class="match-item"><img src="1.png" alt="" width="100%" /></li><li class="match-item"><img src="1.png" alt="" width="100%" /></li></ul><div class="turn-right"><span class="arrow-right"></span></div><div class="clearfix"></div></div></div>
</div>

js

$(".match-item").click(function(){$(".match-item-active").prop('class','match-item');$(this).prop('class','match-item-active');
});$(".turn-right").click(function(){var list = $(".match-list");if(list.scrollWidth - list.scrollLeft() < 1177){list.animate({scrollLeft: list.scrollWidth},1000);}else{list.animate({scrollLeft:list.scrollLeft()+1177},1000);}
});$(".turn-left").click(function(){var list = $(".match-list");if(list.scrollLeft() < 1177){list.animate({scrollLeft:0},1000);}else{list.animate({scrollLeft:list.scrollLeft()-1177},1000);}
});

css

ul,li{list-style: none;margin: 0;padding: 0;
}.match .slide{margin: 40px 0;
}.match .turn-left,.match .turn-right{display: inline-block;vertical-align: middle;width: 80px;height: 400px;cursor: pointer;margin: 0 20px;
}.match .turn-left:hover{background: linear-gradient(to left,rgba(96,110,122,0.8),rgba(96,110,122,0));
}.match .turn-right:hover{background: linear-gradient(to right,rgba(96,110,122,0.8),rgba(96,110,122,0));
}.match .turn-left .arrow-left,.match .turn-right .arrow-right{position: relative;
}.match .turn-left .arrow-left:before{content: '';display: inline-block;width: 50px;height: 10px;transform: skewY(-45deg);background-color: #eee;position: absolute;top: 150px;left: 15px;
}.match .turn-left .arrow-left:after{content: '';display: inline-block;width: 50px;height: 10px;transform: skewY(45deg);background-color: #eee;position: absolute;top: 200px;left: 15px;
}.match .turn-right .arrow-right:before{content: '';display: inline-block;width: 50px;height: 10px;transform: skewY(45deg);background-color: #eee;position: absolute;top: 150px;left: 15px;
}.match .turn-right .arrow-right:after{content: '';display: inline-block;width: 50px;height: 10px;transform: skewY(-45deg);background-color: #eee;position: absolute;top: 200px;left: 15px;
}.match .match-list{width: 1100px;height: 405px;padding: 20px 0;vertical-align: middle;display: inline-block;white-space: nowrap;overflow-x: hidden;overflow-y: hidden;
}.match .match-item{width: 270px;height: 405px;display: inline-block;margin: 0 10px 10px;box-shadow: 0 1px 6px 2px rgba(1,1,1,0.6);border-radius: 2px;border: 2px solid rgba(1,1,1,0.6);transition: all 0.5s;position: relative;cursor: pointer;
}.match .match-item:hover{transform: scale(1.05);box-shadow: 0 1px 8px 4px rgba(1,1,1,0.8);border: 2px solid rgba(1,1,1,0.8);
}.match .match-item-active{width: 270px;height: 405px;transform: scale(1.05);display: inline-block;margin: 0 10px 10px;box-shadow: 0 1px 10px 4px rgba(1,1,1,0.8);border-radius: 2px;border: 2px solid #68d0ff;position: relative;cursor: pointer;
}.match .info{position: absolute;left: 0;bottom: 0;width: 250px;height: 0;overflow: hidden;padding: 0 10px;z-index: 200;transition: all 0.5s;background-color: rgba(0,0,0,0.6);
}.match .match-item-active .info{width: 250px;height: 120px;
}.match .match-item:hover > .info{height: 120px;
}.match .info p{font-size: 16px;font-family: SourceHanSansCN-Medium, sans-serif;color: #f0f0f0;margin: 5px 0;padding: 0;line-height: 30px;height: 30px;text-shadow: 1px 1px rgba(1,1,1,0.6);
}.match .info .color-blue{color: #00a1fe;font-size: 18px;
}.match .info .color-yellow{color: #e07e3e;font-size: 18px;
}

这篇关于使用jq和css3自制横向左右滑动图片列表的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected