js点击拉拽轮播图pc端移动端适配
<div class="content">
<button class="left">left</button>
<button class="right">right</button>
<div class="index"></div>
<div class="lists">
<!--<!–width: item的数量÷3乘以100%–>-->
<div class="box">
<!--width: 1÷item的数量乘以100%-->
<div class="item">
<img src="a.png" alt="a">
<p>aaa</p>
</div>
<div class="item active">
<img src="b.png" alt="b">
<p>bbb</p>
</div>
<div class="item">
<img src="c.png" alt="c">
<p>ccc</p>
</div>
<div class="item">
<img src="d.png" alt="d">
<p>ddd</p>
</div>
<div class="item">
<img src="e.png" alt="e">
<p>eee</p>
</div>
<div class="item">
<img src="f.png" alt="f">
<p>ffff</p>
</div>
</div>
</div>
</div>
<script>
$(function(){
// 循环数据 假设有个数组,有10条数据
// var arr = [1,2,3,4,5,6,7,8,9,10];
// var arr_len = arr.length;
// var box = '<div class="box" style="width: '+arr_len/3*100+'%;"></div>';
// $('.lists').append(box);
// for (var i = 0; i < arr_len;i++){
// var newDiv = document.createElement('div');
// newDiv.innerHTML = '<img src=""/>'+
// '<p>' +
// (i+1)+
// '</p>';
// newDiv.className = 'item '+(i==1?'active':'');
// newDiv.style = 'width: '+1/arr_len*100+'%;';
// newDiv.onclick = (function(ind) {
// return function () {
// index = ind-1;
// console.log(ind)
// $(".box").animate({left: -index*100/3+"%"})
// $(".item").removeClass('active')
// $($(".item")[index+1]).addClass('active');
// $('.index').text(index+2)
// }
// })(i) ;
// $('.box').append(newDiv);
// } var arr_len = $('.item').length; $('.box').css({width: arr_len/3*100+'%'})
$('.item').css({width: 1/arr_len*100+'%'}) $('.item').on('click',function (e) {
var _this = $(e.target);
if (!_this.hasClass('item')){
_this = _this.parents('.item');
}
index = _this.index('.item')-1;
$(".box").animate({left: -index*100/3+"%"})
$(".item").removeClass('active')
$($(".item")[index+1]).addClass('active');
$('.index').text(index+2)
}) var index = 0;
var len = arr_len;
var temp = true;
var pageX,pageY;
$('.content').on('touchstart',function (e) {
var touches = e.originalEvent.targetTouches[0];
pageX = touches.pageX;
pageY = touches.pageY;
}).on('touchmove',function (e) {
var touches = e.originalEvent.targetTouches[0];
if (pageX>touches.pageX+20){
left()
}else if (pageX<touches.pageX-20){
right()
}
})
$(".left").on('click',left)
$(".right").on('click',right)
function left() {
console.log(index,temp,'left')
if (index < len-2&&temp){
index++;
move(index)
}
}
function right() {
if (index > 0&&temp){
index--;
move(index)
}
}
function move(index) {
if (temp){
temp = false;
var left = $(".box").offset().left;
$(".box").animate({left: -index*100/3+"%"},function () {
temp = true;
})
$(".item").removeClass('active')
$($(".item")[index+1]).addClass('active');
$('.index').text(index+2)
} } })
.lists {
position: relative;
height: 100px;
overflow: hidden;
}
.box {
position: absolute;
}
.item {
float: left;
background: #008000;
height: 100px;
}
.item img {
width: 20px;
display: block;
margin: 0 auto;
}
.item p {
text-align: center;
}
.item.active {
background: #0000FF;
font-size: 30px;
}
.item.active img {
width: 40px;
}
.item.active p {
font-size: 30px;
}
js点击拉拽轮播图pc端移动端适配的更多相关文章
- 原生JS面向对象思想封装轮播图组件
原生JS面向对象思想封装轮播图组件 在前端页面开发过程中,页面中的轮播图特效很常见,因此我就想封装一个自己的原生JS的轮播图组件.有了这个需求就开始着手准备了,代码当然是以简洁为目标,轮播图的各个功能 ...
- js 基础篇(点击事件轮播图的实现)
轮播图在以后的应用中还是比较常见的,不需要多少行代码就能实现.但是在只掌握了js基础知识的情况下,怎么来用较少的而且逻辑又简单的方法来实现呢?下面来分析下几种不同的做法: 1.利用位移的方法来实现 首 ...
- 原生js实现响应式轮播图,支持电脑端点击切图,手机端滑动切图
轮播图的实现原理并不难,但是步骤有些繁琐.最近练习了一个轮播图,大部分是跟着网上的教程写的,然后自己做了一点兼容ie8的修改,加了点击切换图片的特效和手机端的滑动特效,让这个轮播图可以在响应式的网站中 ...
- 自己用原生JS写的轮播图,支持移动端触摸滑动,分页器圆点可以支持mouseover鼠标移入和click点击,高手看了勿喷哈
自己用原生JavaScript写的轮播图,分页器圆点按钮可支持click点击,也可支持mouseover鼠标悬浮触发,同时支持移动端触摸滑动,有兴趣的友友可以试试哈,菜鸟一枚,高手看了勿喷,请多多指正 ...
- JS+css3焦点轮播图PC端
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 原生js写一个无缝轮播图插件(支持vue)
轮播图插件(Broadcast.js) 前言:写这个插件的原因 前段时间准备用vue加上网易云的nodejs接口,模拟网易云音乐移动端.因为想自己写一遍所有的代码以及加固自己的flex布局,所以没有使 ...
- 用html +js+css 实现页面轮播图效果
html 页面 <html lang="en"> <head> <meta charset="UTF-8"> <met ...
- 自己用原生JS写的轮播图,支持移动端触屏滑动,面向对象思路。分页器圆点支持click和mouseover。
自己用原生javascript写的轮播图,面向对象思路,支持移动端手指触屏滑动.分页器圆点可以选择click点击或mouseover鼠标移入时触发.图片滚动用的setInterval,感觉setInt ...
- 用js和jQuery做轮播图
Javascript或jQuery做轮播图 css样式 <style> a{ text-decoration:none; } .naver{ width: 100%; position:r ...
随机推荐
- Luogu P4478 [BJWC2018]上学路线 卢卡斯+组合+CRT
首先,从$(0,0)$走到$(n,m)$的方案数是$ C_{n+m}^n$,可以把走的方向看作一种序列,这个序列长$ n+m$ ,你需要从中任取$n$个位置,让他向右走: 然后就是如何处理不能走的点: ...
- 1100 Mars Numbers(20 分)
People on Mars count their numbers with base 13: Zero on Earth is called "tret" on Mars. T ...
- list倒序删除
public static void main(String[] args) { List<Integer> nums = new ArrayList<Integer>(); ...
- 查询时根据权限更改sql
import java.lang.reflect.Method; import org.apache.log4j.Logger; import org.springframework.aop.Meth ...
- 使用C#连接 MyCat 链接串
所属专栏: mycat的安装部署以及监控和运维 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u014180504/article/detai ...
- Spark Mllib里的协调过滤的概念和实现步骤、LS、ALS的原理、ALS算法优化过程的推导、隐式反馈和ALS-WR算法
不多说,直接上干货! 常见的推荐算法 1.基于关系规则的推荐 2.基于内容的推荐 3.人口统计式的推荐 4.协调过滤式的推荐 (广泛采用) 协调过滤的概念 在现今的推荐技术和算法中,最被大家广泛认可和 ...
- 一个关于document.write()的问题
Index.html中: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...
- hibernate课程 初探单表映射1-6 hibernate项目建立以及导入jar包
hibernate 项目建立 1 new ==>java project hibernate 导入jar包 1 windows==>prerence==>java ==>bui ...
- vue-cli建立的项目如何在手机端运行以及如何用charles来抓包
刚开始自己在config文件夹下的index.js中的dev下的host写成的是localhost,但是发现自己不能在手机端访问,并且也不可以在charles进行抓包处理,后来把localhost改成 ...
- C# 执行可执行文件
可以用C#脚本执行可执行文件,一般可以用C# IO流写出.bat脚本,然后顺带执行脚本,然后滑稽.三连... Process proc = null; try { proc = new Process ...