js 原生轮播图插件
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<style>
#swiper{
height: 200px;
width: 200px;
border: 1px solid #cccccc;
margin: 0 auto;
overflow: hidden;
position: relative;
}
#swiper div{
height: 200px;
width: 200px;
float: left;
text-align: center; }
#swiper div:nth-child(1){
background: pink;
}
#swiper div:nth-child(2){
height: 200px;
width: 200px;
float: left;
text-align: center;
background: gray;
}
#swiper div:nth-child(3){
height: 200px;
width: 200px;
float: left;
text-align: center;
background: yellow;
}
#swiper div:nth-child(4){
height: 200px;
width: 200px;
float: left;
text-align: center;
background: pink;
}
#swiper div:nth-child(5){
height: 200px;
width: 200px;
float: left;
text-align: center;
background: gray;
}
#left{
position: absolute;
left: 0;
top: 50%;
transform: translate3d(0,-50%,0);
z-index: 999;
}
#right{
position: absolute;
right: 0;
top: 50%;
transform: translate3d(0,-50%,0);
z-index: 999;
}
#swiperson{
position: relative;
}
</style>
<title>Document</title>
</head>
<body>
<div id="swiper">
<button id="left">左</button>
<button id="right">右</button>
<div id="swiperson">
<div>3</div>
<div>1</div>
<div>2</div>
<div>3</div>
<div>1</div>
</div>
</div>
<script>
// 轮播图插件 // 样式布局
let allWidth = document.getElementById('swiperson').children.length * document.getElementById('swiper').clientWidth;
let oneWidth = document.getElementById('swiper').clientWidth;
document.getElementById('swiperson').style.width = allWidth + 'px';
document.getElementById('swiperson').style.transform = 'translateX(-'+ oneWidth +'px)';
// 点击逻辑
let index = 1;
let length = document.getElementById('swiperson').children.length;
let flg = true; document.getElementById("swiperson").addEventListener("transitionend", function () {
flg = true;
if(index === length-1){
document.getElementById('swiperson').style.transition = 'none';
document.getElementById('swiperson').style.transform = 'translateX(-'+ oneWidth +'px)';
index = 1
}
if(index === 0){
document.getElementById('swiperson').style.transition = 'none';
document.getElementById('swiperson').style.transform = 'translateX(-'+ oneWidth * (length-2)+'px)';
index = length - 2
}
}); function right() {
if(flg === true){
flg = false;
index++;
document.getElementById('swiperson').style.transition = 'all 1s';
document.getElementById('swiperson').style.transform = 'translateX(-'+ oneWidth*index +'px)'
}
} function left() {
if(flg === true){
flg = false;
index--;
document.getElementById('swiperson').style.transition = 'all 1s';
document.getElementById('swiperson').style.transform = 'translateX(-'+ oneWidth*index +'px)'
}
} // 点击
document.getElementById('left').onclick = function () {
left()
};
document.getElementById('right').onclick = function () {
right();
}; // 自动轮播
var time = setInterval(function () {
right();
},2000) //手动滑动逻辑
var a;
document.getElementById('swiper').ontouchstart = function (ev) {
clearInterval(time)
a = ev.changedTouches[0].pageX;
}; document.getElementById('swiper').ontouchmove = function (ev) {
let b = ev.changedTouches[0].pageX;
console.log(b)
if(b - a > 40){
console.log('右滑动')
left()
}
if(a - b > 40){
console.log('左滑动')
right(); }
} //PC 端 滑动逻辑
var a;
document.getElementById('swiper').onmousedown = function (ev) {
clearInterval(time)
console.log(ev)
a = ev.pageX; document.getElementById('swiper').onmousemove = function (ev) {
console.log('move');
let b = ev.pageX;
if(b - a > 0){
console.log('右滑动')
left()
}
if(a - b > 0){
console.log('左滑动')
right();
}
};
} document.getElementById('swiper').onmouseup = function (ev) {
document.getElementById('swiper').onmousemove = null
} </script>
</body>
</html>
js 原生轮播图插件的更多相关文章
- 封装一个简单的原生js焦点轮播图插件
轮播图实现的效果为,鼠标移入左右箭头会出现,可以点击切换图片,下面的小圆点会跟随,可以循环播放(为了方便理解,没有补2张图做无缝轮播).本篇文章的主要目的是分享封装插件的思路. 轮播图我一开始是写成非 ...
- js原生轮播图
轮播图是新手学前端的必经之路! 直接上代码! <!DOCTYPE html><html lang="en"><head> <meta ch ...
- featureCarousel.js 3d轮播图插件
jQuery Feature Carousel 插件是国外的一比较优秀的旋转木马图片插件. 点击这里进入原文. 插件特点: 1.处理div的3d旋转木马效果. 2.支持一个中心,2个侧面的功能 3.中 ...
- 小白之js原生轮播图
html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- 原生js写一个无缝轮播图插件(支持vue)
轮播图插件(Broadcast.js) 前言:写这个插件的原因 前段时间准备用vue加上网易云的nodejs接口,模拟网易云音乐移动端.因为想自己写一遍所有的代码以及加固自己的flex布局,所以没有使 ...
- 原生JavaScript(js)手把手教你写轮播图插件(banner)
---恢复内容开始--- 1.轮播图插件 1.什么是插件: 为已有的程序增加功能 2.插件的特点(为什么要做成一个插件)与注意事项: 1.通用性,可移植性强 2.兼容性:不会对其他代码产生影响 3.创 ...
- 原生js焦点轮播图
原生js焦点轮播图主要注意这几点: 1.前后按钮实现切换,同时注意辅助图2.中间的button随着前后按钮对应切换,同时按button也能跳转到相应的index3.间隔调用与无限轮播.4.注意在动画时 ...
- 原生js实现轮播图
原生js实现轮播图 很多网站上都有轮播图,但找到一个系统讲解的却很难,因此这里做一个简单的介绍,希望大家都能有所收获,如果有哪些不正确的地方,希望大家可以指出. 原理: 将一些图片在一行中平铺,然后计 ...
- 学习笔记: js插件 —— SuperSlide 2 (轮播图插件,PC用)
SuperSlide 2 轮播图插件,较老.但还好用. 适用于PC,是绑定到jquery上的方法: $.slide(); 如果在实际中找不到.slide方法,请检查jquery等.js文件的引入次序 ...
随机推荐
- linux设置网络三种方法
http://blog.csdn.net/u010003835/article/details/52233296
- ZJNU 1426 - YNingC的困惑
注意到N最大只有1e6,但是xy最大有2e8,直接模拟2e8会超时 所以可以将1e6个区间离散化后模拟,模拟时的最坏情况为2e6满足题意 /* Written By StelaYuri */ #inc ...
- ant design for vue select 数据回显问题
例如: 想要回显id为1的温度, 结果直接在select框中显示了1,而不是选中了温度, 此时因为select中的value是string类型, 而我们设置的id是number类型, 对应不上, 所以 ...
- 3.windows-oracle实战第三课 -表的管理
oracle的核心 多表查询.存储过程.触发器 字符型: char 定义 最大2000字符,例如“char(10) '小韩' 前4个字符放小韩,后添加6个空格补全,查询极快 varchar2(2 ...
- numpy 加速 以及 ipython
先安装openblas, 然后用pip 安装numpy sudo ln -s /usr/lib64/libopenblas-r0.2.14.so /usr/lib64/libopenblas.so 为 ...
- ZZJ_淘淘商城项目:day04(淘淘商城03 - 前台系统搭建、实现、内容管理系统实现)
1. 今日大纲 1. 实现商品的编辑 2. 实现商品的规格参数功能 3. 搭建前台系统 4. 实现首页商品类目的显示 2.2.4. 未实现TODO 编辑时图片回显: 思路: 1. 查 ...
- Java——Collection集合、迭代器、泛型
集合 ——集合就是java提供的一种容器,可以用来存储多个数据. 集合和数组的区别 数组的长度是固定的.集合的长度是可变的. 数组中存储的是同一类型的元素,可以存储基本数据类型值. 集合存储的都是对象 ...
- IIS设置禁止某个IP或IP段访问网站的方法
网站被刷,对话接不过来 打开IIS,选中禁IP的站点,找到“ip地址和域限制”这个功能,如果没有安装,打开服务器管理器,点击角色,窗口右边找到添加角色服务,找到“IP和域限制”并勾选安装. 打开ip地 ...
- redhat下libreoffice 的安装
1.第一次安装libreoffic时是用网络yum源安装的,但是装好之后不能用,找了好久没有找出问题,后来从官网下载安装包后安装就可以了. 下载地址:https://zh-cn.libreoffice ...
- Perl: print @globbing."\n"; 和 print @globbing; 不一样,一个已经转换为数组元素个数了
48 print @globbing."\n"; 输出: 3