目录
代码段
目标效果:
1.利用定时器(setInterval)自动轮播图片
2.等着1.中过渡完成之后,再去判断 监听过渡完成的事件 transitionend
实现无缝滚动
(第一个focus1的index是0,最后一个focus1的index是3。当index变为3或更大的时候,跳转回index为0;当index<0的时候,跳转回index为2)
3.ul滚动的时候,小圆点跟随变化
4.手指可以滑动轮播图(本质上是让ul跟随手指移动)
(触摸元素(touchstart)获得手指初始坐标,手指滑动(touchmove)获得手指移动距离,盒子最终移动距离=盒子之前的移动距离+手指移动的距离)
5.在4.中手指滑动轮播图,手指移开(touchend)的时候,如果用户手指移动过再去判断,不然不判断
(1)如果移动距离大于50px,则播放上一张/下一张
(2)如果移动距离小于50px,则回弹
手指移开(touchend)后,重新开启定时器,让ul继续自动轮播
6.当窗口滚到nav的地方,显示goBack返回顶部图片
7.点击goBack图片,滚动窗口至页面最上方 window.scroll(0,0);




(4)自动播放的无缝滚动原理:

e.g.轮播图效果见图片的红框中

- window.addEventListener('load', function () {//等整个页面加载完毕,再运用js
- // 1.获取元素
- var focus = document.querySelector('.focus');
- var ul = focus.children[0];
- var ol = focus.children[1];
- // 移动的每张图片的宽度,就是focus宽度
- // 获得focus的宽度
- var w = focus.offsetWidth;
- // 2.利用定时器自动轮播图片
- // 声明一个变量index来控制ul每次移动
- var index = 0;
- var timer = setInterval(function () {
- index++;
- var translatex = -index * w;//因为是往左走,所以是负值
- ul.style.transition = 'all .3s';//添加过渡,让图片慢点自动滚动
- ul.style.transform = 'translateX(' + translatex + 'px)';//每隔2S,往左滚动一张图片
- }, 2000);
- // 等着过渡完成之后,再去判断 监听过渡完成的事件 transitionend
- ul.addEventListener('transitionend', function () {
- // 无缝滚动
- // 第一个focus1的index是0,最后一个focus1的index是3
- // 当index变为3或更大的时候,跳转回index为0
- if (index >= 3) {
- index = 0;
- // 去掉过渡,直接跳转
- ul.style.transition = 'none';
- // 用最新的index*宽度,继续滚动图片
- var translatex = -index * w;
- ul.style.transform = 'translateX(' + translatex + 'px)';
- } else if (index < 0) {
- // 当index<0的时候,跳转回index为2
- index = 2;
- // 去掉过渡,直接跳转
- ul.style.transition = 'none';
- // 用最新的index*宽度,继续滚动图片
- var translatex = -index * w;
- ul.style.transform = 'translateX(' + translatex + 'px)';
- }
- // 3.小圆点跟随变化效果
- // 移除(classList.remove)ol中li中有current类名的li
- ol.querySelector('li.current').classList.remove('current');
- // 给当前index的li添加(classList.add)类名current
- ol.children[index].classList.add('current');
-
- });
- // 4.手指滑动轮播图
- // 本质上是让ul跟随手指移动
- // 触摸元素(touchstart)获得手指初始坐标
- // 此处是左右滑动,所以只需要X坐标
- var startX = 0;
- var moveX = 0;//之后会使用到该移动距离,所以要是全局变量
- var flag = false;//声明一个全局变量flag
- ul.addEventListener('touchstart', function (e) {
- startX = e.targetTouches[0].pageX;
- // 手指滑动轮播图的时候,要停止轮播图的自动滚动
- // 当手指按下时候,就停止图片自动滚动
- clearInterval(timer);
- });
-
- // 手指滑动(touchmove)获得手指移动距离
- ul.addEventListener('touchmove', function (e) {
- // 手指移动距离=手指滑动时的手指X坐标-手指初始X坐标
- moveX = e.targetTouches[0].pageX - startX;
- // 盒子最终移动距离=盒子之前的移动距离+手指移动的距离
- var translatex = -index * w + moveX;
- // 手指移动的时候不需要过渡效果,所以去掉过渡,直接跳转
- ul.style.transition = 'none';
- ul.style.transform = 'translateX(' + translatex + 'px)';
- flag = true;//如果用户手指移动过再去判断,不然不判断
- e.preventDefault();//阻止滚动ul的时候,让屏幕滚动的行为
- });
- // 手指移开(touchend)根据移动距离判断是 回弹 还是 播放上一张/下一张
- ul.addEventListener('touchend', function (e) {
- if (flag) {//如果用户手指移动过再去判断,不然不判断
- // (1)如果移动距离大于50px,则播放上一张/下一张
- if (Math.abs(moveX) > 50) {
- // 如果是右滑,则播放下一张,moveX是正值
- if (moveX > 0) {
- index--;
- } else {
- // 如果是左滑,则播放上一张,moveX是负值
- index++;
- }
- // 用最新的index*宽度,继续滚动图片
- var translatex = -index * w;
- // 图片滚动时要有过渡效果
- ul.style.transition = 'all .3s';
- ul.style.transform = 'translateX(' + translatex + 'px)';
- } else {
- // (2)如果移动距离小于50px,则回弹
- // 用最新的index*宽度,继续滚动图片
- var translatex = -index * w;
- // 图片滚动时要有过渡效果
- ul.style.transition = 'all .1s';
- ul.style.transform = 'translateX(' + translatex + 'px)';
- }
-
- }
- // 手指离开的时候就重新开启定时器
- clearInterval(timer);//先清除定时器,保证页面中只有一个定时器
- //再开启定时器
- timer = setInterval(function () {
- index++;
- var translatex = -index * w;
- ul.style.transition = 'all .3s';
- ul.style.transform = 'translateX(' + translatex + 'px)';
- }, 2000);
- });
- // 返回顶部模块制作
- var goBack = document.querySelector('.goBack');
- var nav = document.querySelector('nav');
- window.addEventListener('scroll', function () {
- if (window.pageYOffset >= nav.offsetTop) {
- // 当窗口滚到nav的地方,显示goBack返回顶部图片
- goBack.style.display = 'block';
- } else {
- goBack.style.display = 'none';
- }
- });
- // 点击goBack图片,滚动窗口至页面最上方
- goBack.addEventListener('click', function () {
- window.scroll(0, 0);
- });
-
- })





body {
max-width: 540px;
min-width: 320px;
margin: 0 auto;
font: normal 14px/1.5 Tahoma, "Lucida Grande", Verdana, "Microsoft Yahei", STXihei, hei;
color: #000;
background: #f2f2f2;
overflow-x: hidden;
-webkit-tap-highlight-color: transparent;
}
ul {
list-style: none;
margin: 0;
padding: 0;
}
a {
text-decoration: none;
color: #222;
}
div {
box-sizing: border-box;
}
/* 搜索模块 */
.search-index {
display: flex;
/* 固定定位跟父级没有关系 它以屏幕为准 */
position: fixed;
top: 0;
left: 50%;
/* 设置z-index让搜索模块始终在最上层 */
z-index: 999;
/* 固定的盒子应该有宽度 */
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
width: 100%;
min-width: 320px;
max-width: 540px;
height: 44px;
/* background-color: pink; */
background-color: #F6F6F6;
border-top: 1px solid #ccc;
border-bottom: 1px solid #ccc;
}
.search {
position: relative;
height: 26px;
line-height: 24px;
border: 1px solid #ccc;
flex: 1;
font-size: 12px;
color: #666;
margin: 7px 10px;
padding-left: 25px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, .2);
}
.search::before {
content: "";
position: absolute;
top: 5px;
left: 5px;
width: 15px;
height: 15px;
background: url(../images/sprite.png) no-repeat -59px -279px;
background-size: 104px auto;
}
.user {
width: 44px;
height: 44px;
/* background-color: purple; */
font-size: 12px;
text-align: center;
color: #2eaae0;
}
.user::before {
content: "";
display: block;
width: 23px;
height: 23px;
background: url(../images/sprite.png) no-repeat -59px -194px;
background-size: 104px auto;
margin: 4px auto -2px;
}
/* goBack */
.goBack {
display: none;
position: fixed;
bottom: 50px;
right: 20px;
width: 38px;
height: 38px;
background: url(../images/back.png) no-repeat;
background-size: 38px 38px;
}
/* focus */
.focus {
position: relative;
padding-top: 44px;
overflow: hidden;
}
.focus img {
width: 100%;
}
.focus ul {
width: 500%;
margin-left: -100%;
}
.focus ul li {
float: left;
width: 20%;
}
.focus ol {
position: absolute;
bottom: -9px;
right: 5px;
}
.focus ol li {
display: inline-block;
width: 5px;
height: 5px;
background-color: #fff;
list-style: none;
border-radius: 2px;
/* 给ol里的li也就是小圆点加transition过渡,让小圆点的变化更自然 */
transition: all .3s;
}
.focus ol li.current {
width: 15px;
}
/* local-nav */
.local-nav {
display: flex;
height: 64px;
margin: 3px 4px;
background-color: #fff;
border-radius: 8px;
}
.local-nav li {
flex: 1;
}
.local-nav a {
display: flex;
flex-direction: column;
/* 侧轴居中对齐 因为是单行 */
align-items: center;
font-size: 12px;
}
.local-nav li [class^="local-nav-icon"] {
width: 32px;
height: 32px;
background-color: pink;
margin-top: 8px;
background: url(../images/localnav_bg.png) no-repeat 0 0;
background-size: 32px auto;
}
.local-nav li .local-nav-icon-icon2 {
background-position: 0 -32px;
}
.local-nav li .local-nav-icon-icon3 {
background-position: 0 -64px;
}
.local-nav li .local-nav-icon-icon4 {
background-position: 0 -96px;
}
.local-nav li .local-nav-icon-icon5 {
background-position: 0 -128px;
}
/* nav */
nav {
overflow: hidden;
border-radius: 8px;
margin: 0 4px 3px;
}
.nav-common {
display: flex;
height: 88px;
background-color: pink;
}
.nav-common:nth-child(2) {
margin: 3px 0;
}
.nav-items {
/* 不冲突的 */
flex: 1;
display: flex;
flex-direction: column;
}
.nav-items a {
flex: 1;
text-align: center;
line-height: 44px;
color: #fff;
font-size: 14px;
/* 文字阴影 */
text-shadow: 1px 1px rgba(0, 0, 0, .2);
}
.nav-items a:nth-child(1) {
border-bottom: 1px solid #fff;
}
.nav-items:nth-child(1) a {
border: 0;
background: url(../images/hotel.png) no-repeat bottom center;
background-size: 121px auto;
}
/* -n+2就是选择前面两个元素 */
.nav-items:nth-child(-n+2) {
border-right: 1px solid #fff;
}
.nav-common:nth-child(1) {
background: -webkit-linear-gradient(left, #FA5A55, #FA994D);
}
.nav-common:nth-child(2) {
background: -webkit-linear-gradient(left, #4B90ED, #53BCED);
}
.nav-common:nth-child(3) {
background: -webkit-linear-gradient(left, #34C2A9, #6CD559);
}
/* subnav-entry */
.subnav-entry {
display: flex;
border-radius: 8px;
background-color: #fff;
margin: 0 4px;
flex-wrap: wrap;
padding: 5px 0;
}
.subnav-entry li {
/* 里面的子盒子可以写 % 相对于父级来说的 */
flex: 20%;
}
.subnav-entry a {
display: flex;
flex-direction: column;
align-items: center;
}
.subnav-entry-icon {
width: 28px;
height: 28px;
background-color: pink;
margin-top: 4px;
background: url(../images/subnav-bg.png) no-repeat;
background-size: 28px auto;
}
/* sales-box */
.sales-box {
border-top: 1px solid #bbb;
background-color: #fff;
margin: 4px;
}
.sales-hd {
position: relative;
height: 44px;
border-bottom: 1px solid #ccc;
}
.sales-hd h2 {
position: relative;
text-indent: -999px;
overflow: hidden;
}
.sales-hd h2::after {
position: absolute;
top: 5px;
left: 8px;
content: "";
width: 79px;
height: 15px;
background: url(../images/hot.png) no-repeat 0 -20px;
background-size: 79px auto;
}
.more {
position: absolute;
right: 5px;
top: 0px;
background: -webkit-linear-gradient(left, #FF506C, #FF6BC6);
border-radius: 15px;
padding: 3px 20px 3px 10px;
color: #fff;
}
.more::after {
content: "";
position: absolute;
top: 9px;
right: 9px;
width: 7px;
height: 7px;
border-top: 2px solid #fff;
border-right: 2px solid #fff;
transform: rotate(45deg);
}
.row {
display: flex;
}
.row a {
flex: 1;
border-bottom: 1px solid #eee;
}
.row a:nth-child(1) {
border-right: 1px solid #eee;
}
.row a img {
width: 100%;
}
- /*! normalize.css v5.0.0 | MIT License | github.com/necolas/normalize.css */
-
- /**
- * 1. Change the default font family in all browsers (opinionated).
- * 2. Correct the line height in all browsers.
- * 3. Prevent adjustments of font size after orientation changes in
- * IE on Windows Phone and in iOS.
- */
- /* Document
- ========================================================================== */
-
- html {
- font-family: sans-serif; /* 1 */
- line-height: 1.15; /* 2 */
- -ms-text-size-adjust: 100%; /* 3 */
- -webkit-text-size-adjust: 100%; /* 3 */
- }
-
- /* Sections
- ========================================================================== */
-
- /**
- * Remove the margin in all browsers (opinionated).
- */
- body {
- margin: 0;
- }
-
- /**
- * Add the correct display in IE 9-.
- */
- article,
- aside,
- footer,
- header,
- nav,
- section {
- display: block;
- }
-
- /**
- * Correct the font size and margin on `h1` elements within `section` and
- * `article` contexts in Chrome, Firefox, and Safari.
- */
- h1 {
- font-size: 2em;
- margin: 0.67em 0;
- }
-
- /* Grouping content
- ========================================================================== */
-
- /**
- * Add the correct display in IE 9-.
- * 1. Add the correct display in IE.
- */
- figcaption,
- figure,
- main { /* 1 */
- display: block;
- }
-
- /**
- * Add the correct margin in IE 8.
- */
- figure {
- margin: 1em 40px;
- }
-
- /**
- * 1. Add the correct box sizing in Firefox.
- * 2. Show the overflow in Edge and IE.
- */
- hr {
- box-sizing: content-box; /* 1 */
- height: 0; /* 1 */
- overflow: visible; /* 2 */
- }
-
- /**
- * 1. Correct the inheritance and scaling of font size in all browsers.
- * 2. Correct the odd `em` font sizing in all browsers.
- */
- pre {
- font-family: monospace, monospace; /* 1 */
- font-size: 1em; /* 2 */
- }
-
- /* Text-level semantics
- ========================================================================== */
-
- /**
- * 1. Remove the gray background on active links in IE 10.
- * 2. Remove gaps in links underline in iOS 8+ and Safari 8+.
- */
- a {
- background-color: transparent; /* 1 */
- -webkit-text-decoration-skip: objects; /* 2 */
- }
-
- /**
- * Remove the outline on focused links when they are also active or hovered
- * in all browsers (opinionated).
- */
- a:active,
- a:hover {
- outline-width: 0;
- }
-
- /**
- * 1. Remove the bottom border in Firefox 39-.
- * 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari.
- */
- abbr[title] {
- border-bottom: none; /* 1 */
- text-decoration: underline; /* 2 */
- text-decoration: underline dotted; /* 2 */
- }
-
- /**
- * Prevent the duplicate application of `bolder` by the next rule in Safari 6.
- */
- b,
- strong {
- font-weight: inherit;
- }
-
- /**
- * Add the correct font weight in Chrome, Edge, and Safari.
- */
- b,
- strong {
- font-weight: bolder;
- }
-
- /**
- * 1. Correct the inheritance and scaling of font size in all browsers.
- * 2. Correct the odd `em` font sizing in all browsers.
- */
- code,
- kbd,
- samp {
- font-family: monospace, monospace; /* 1 */
- font-size: 1em; /* 2 */
- }
-
- /**
- * Add the correct font style in Android 4.3-.
- */
- dfn {
- font-style: italic;
- }
-
- /**
- * Add the correct background and color in IE 9-.
- */
- mark {
- background-color: #ff0;
- color: #000;
- }
-
- /**
- * Add the correct font size in all browsers.
- */
- small {
- font-size: 80%;
- }
-
- /**
- * Prevent `sub` and `sup` elements from affecting the line height in
- * all browsers.
- */
- sub,
- sup {
- font-size: 75%;
- line-height: 0;
- position: relative;
- vertical-align: baseline;
- }
-
- sub {
- bottom: -0.25em;
- }
-
- sup {
- top: -0.5em;
- }
-
- /* Embedded content
- ========================================================================== */
-
- /**
- * Add the correct display in IE 9-.
- */
- audio,
- video {
- display: inline-block;
- }
-
- /**
- * Add the correct display in iOS 4-7.
- */
- audio:not([controls]) {
- display: none;
- height: 0;
- }
-
- /**
- * Remove the border on images inside links in IE 10-.
- */
- img {
- border-style: none;
- }
-
- /**
- * Hide the overflow in IE.
- */
- svg:not(:root) {
- overflow: hidden;
- }
-
- /* Forms
- ========================================================================== */
-
- /**
- * 1. Change the font styles in all browsers (opinionated).
- * 2. Remove the margin in Firefox and Safari.
- */
- button,
- input,
- optgroup,
- select,
- textarea {
- font-family: sans-serif; /* 1 */
- font-size: 100%; /* 1 */
- line-height: 1.15; /* 1 */
- margin: 0; /* 2 */
- }
-
- /**
- * Show the overflow in IE.
- * 1. Show the overflow in Edge.
- */
- button,
- input { /* 1 */
- overflow: visible;
- }
-
- /**
- * Remove the inheritance of text transform in Edge, Firefox, and IE.
- * 1. Remove the inheritance of text transform in Firefox.
- */
- button,
- select { /* 1 */
- text-transform: none;
- }
-
- /**
- * 1. Prevent a WebKit bug where (2) destroys native `audio` and `video`
- * controls in Android 4.
- * 2. Correct the inability to style clickable types in iOS and Safari.
- */
- button,
- html [type="button"], /* 1 */
- [type="reset"],
- [type="submit"] {
- -webkit-appearance: button; /* 2 */
- }
-
- /**
- * Remove the inner border and padding in Firefox.
- */
- button::-moz-focus-inner,
- [type="button"]::-moz-focus-inner,
- [type="reset"]::-moz-focus-inner,
- [type="submit"]::-moz-focus-inner {
- border-style: none;
- padding: 0;
- }
-
- /**
- * Restore the focus styles unset by the previous rule.
- */
- button:-moz-focusring,
- [type="button"]:-moz-focusring,
- [type="reset"]:-moz-focusring,
- [type="submit"]:-moz-focusring {
- outline: 1px dotted ButtonText;
- }
-
- /**
- * Change the border, margin, and padding in all browsers (opinionated).
- */
- fieldset {
- border: 1px solid #c0c0c0;
- margin: 0 2px;
- padding: 0.35em 0.625em 0.75em;
- }
-
- /**
- * 1. Correct the text wrapping in Edge and IE.
- * 2. Correct the color inheritance from `fieldset` elements in IE.
- * 3. Remove the padding so developers are not caught out when they zero out
- * `fieldset` elements in all browsers.
- */
- legend {
- box-sizing: border-box; /* 1 */
- color: inherit; /* 2 */
- display: table; /* 1 */
- max-width: 100%; /* 1 */
- padding: 0; /* 3 */
- white-space: normal; /* 1 */
- }
-
- /**
- * 1. Add the correct display in IE 9-.
- * 2. Add the correct vertical alignment in Chrome, Firefox, and Opera.
- */
- progress {
- display: inline-block; /* 1 */
- vertical-align: baseline; /* 2 */
- }
-
- /**
- * Remove the default vertical scrollbar in IE.
- */
- textarea {
- overflow: auto;
- }
-
- /**
- * 1. Add the correct box sizing in IE 10-.
- * 2. Remove the padding in IE 10-.
- */
- [type="checkbox"],
- [type="radio"] {
- box-sizing: border-box; /* 1 */
- padding: 0; /* 2 */
- }
-
- /**
- * Correct the cursor style of increment and decrement buttons in Chrome.
- */
- [type="number"]::-webkit-inner-spin-button,
- [type="number"]::-webkit-outer-spin-button {
- height: auto;
- }
-
- /**
- * 1. Correct the odd appearance in Chrome and Safari.
- * 2. Correct the outline style in Safari.
- */
- [type="search"] {
- -webkit-appearance: textfield; /* 1 */
- outline-offset: -2px; /* 2 */
- }
-
- /**
- * Remove the inner padding and cancel buttons in Chrome and Safari on macOS.
- */
- [type="search"]::-webkit-search-cancel-button,
- [type="search"]::-webkit-search-decoration {
- -webkit-appearance: none;
- }
-
- /**
- * 1. Correct the inability to style clickable types in iOS and Safari.
- * 2. Change font properties to `inherit` in Safari.
- */
- ::-webkit-file-upload-button {
- -webkit-appearance: button; /* 1 */
- font: inherit; /* 2 */
- }
-
- /* Interactive
- ========================================================================== */
-
- /*
- * Add the correct display in IE 9-.
- * 1. Add the correct display in Edge, IE, and Firefox.
- */
- details, /* 1 */
- menu {
- display: block;
- }
-
- /*
- * Add the correct display in all browsers.
- */
- summary {
- display: list-item;
- }
-
- /* Scripting
- ========================================================================== */
-
- /**
- * Add the correct display in IE 9-.
- */
- canvas {
- display: inline-block;
- }
-
- /**
- * Add the correct display in IE.
- */
- template {
- display: none;
- }
-
- /* Hidden
- ========================================================================== */
-
- /**
- * Add the correct display in IE 10-.
- */
- [hidden] {
- display: none;
- }