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文件的引入次序 ...
随机推荐
- PAT Advanced 1145 Hashing – Average Search Time (25) [哈希映射,哈希表,平⽅探测法]
题目 The task of this problem is simple: insert a sequence of distinct positive integers into a hash t ...
- SQL查询出一个表数据插入到另一个表里
下面两中方式都是将 srcTbl 的数据插入到 destTbl,但两句又有区别的: 方式一 (select into from)要求目标表(destTbl)不存在,因为在插入时会自动创建. selec ...
- ubuntu 深度学习cuda环境搭建,docker-nvidia 2019-02
ubuntu 深度学习cuda环境搭建 ubuntu系统版本 18.04 查看GPU型号(NVS 315 性能很差,比没有强) 首先最好有ssh服务,以下操作都是远程ssh执行 lspci | gre ...
- shell里脚本里写个简单的函数,显示颜色
如果多次用到颜色显示,还是定义一个函数比较实在,具体什么颜色上网找一下 #!/bin/bash # 定义一个红色显示的函数 function echo_red () { local what=$* e ...
- C++各种模板
高精度: 重载运算符版: #include<cstdio> #include<cstring> #include<algorithm> using namespac ...
- Q_Go1
Go语言的特点及优势 一.Go语言设计初衷(为什么设计Go语言?) 1.1.设计Go源是为了解决当时Google开发遇到的困难: 大量的C++代码,同时有引入了Java和Python 成千上万的工程师 ...
- yum源本地部署完后网络部署报错
错误信息 已加载插件:fastestmirror Determining fastest mirrors * base: mirrors.aliyun.com * extras: mirrors.al ...
- SGD/BGD/MBGD使用python简单实现
算法具体可以参照其他的博客: 随机梯度下降: # coding=utf-8 ''' 随机梯度下降 ''' import numpy as np # 构造训练数据 x = np.arange(0., 1 ...
- DAG Optimal Coin Change
题目描述 In a 10-dollar shop, everything is worthy 10 dollars or less. In order to serve customers more ...
- wait操作接口
1.等待回收的两个函数wait()和waitpid()函数 1.1 wait(int *status)的用法:阻塞函数,等待任意一个子进程的返回. *wait(NULL):对子进程的结束状态不关心 ...