移动端图片无缝滚动是如何实现的?(代码)


本文摘自PHP中文网,作者PHPzhong,侵删。

本篇文章给大家带来的内容是关于移动端图片无缝滚动是如何实现的?(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

<!doctype html>

<html>

<head>

<meta charset="UTF-8" />

<meta name="viewport" content="width=device-width,user-scalable=no">

<title>Document</title>

<script type="text/javascript">

//获取html

var html = document.documentElement;

//设置html的字体大小 = 可视区的宽度 / 15

html.style.fontSize = html.clientWidth / 15 + 'px';

//阻止pc和浏览器默认行为。

document.addEventListener('touchstart',function(ev){

ev.preventDefault();

});

</script>

<style>

body{

margin: 0;

}

.wrap{

height: 4.68rem;

position: relative;

}

.list{

padding: 0;

margin: 0;

width:400%;

position: absolute;

top:0;

left:0;

list-style: none;

}

.list li{

float: left;

width:15rem;

}

.list img{

width:15rem;

display: block;

}

nav{

width:15rem;

height: 10px;

position: absolute;

bottom:5px;

z-index: 1;

text-align:center;

}

nav a{

width:15px;

height: 15px;

display: inline-block;

background: red;

border-radius:50%;

vertical-align:top;

}

nav a.active{

background:#fff;

}

</style>

</head>

<body>

<section>

<ul>

<li>

<img src="img/img.jpg" alt="" />

</li>

<li>

<img src="img/img2.jpg" alt="" />

</li>

<li>

<img src="img/img3.jpg" alt="" />

</li>

<li>

<img src="img/img4.jpg" alt="" />

</li>

</ul>

<nav>

<a href="javascript:;"></a>

<a href="javascript:;"></a>

<a href="javascript:;"></a>

<a href="javascript:;"></a>

</nav>

</section>

<script type="text/javascript">

var wrap=document.getElementsByClassName("wrap")[0],

list=document.getElementsByClassName("list")[0],

disX=0,

listL=0,

n=0,

w=wrap.clientWidth,

s=0,

timer=0;

a=document.getElementsByTagName("a");

list.addEventListener("touchstart",start);

list.addEventListener("touchmove",move);

list.addEventListener("touchend",end);

list.innerHTML+=list.innerHTML;

len=list.children.length;

console.log(len);

list.style.width=w*len+"px";

function start(ev){

clearInterval(timer);

var e=ev.changedTouches[0];

disX=e.pageX;

list.style.transition="none";

var num=Math.round(list.offsetLeft/w);

list.style.left=num*w+"px";

if(num==0){

num=a.length;

list.style.left=-num*w+"px";

}

if(-num==len-1){

num=a.length-1;

list.style.left=-num*w+"px";

}

listL=this.offsetLeft;

}

function move(ev){

var e=ev.changedTouches[0];

list.style.left=(e.pageX-disX)+listL+"px";

}

function end(){

var num=Math.round(list.offsetLeft/w);

console.log(num)

list.style.transition="0.5s";

list.style.left=num*w+"px";

a[n].className="";

a[(-num)%a.length].className="active";

n=(-num)%a.length;

timer=setInterval(function(){

inn();

},1000)

s=-num;

}

timer=setInterval(function(){

inn();

},1000)

function inn(){

s++;

list.style.left=-s*w+"px";

list.style.transition="0.5s";

console.log(s);

if(s>len-1){

s=a.length-1;

list.style.transition="none";

list.style.left=-s*w+"px";

console.log(list.style.left);

setTimeout(function(){

list.style.transition="0.5s";

s++;

list.style.left=-s*w+"px";

a[n].className="";

a[s%a.length].className="active";

n=s%a.length;

},30)

}

a[n].className="";

a[s%a.length].className="active";

n=s%a.length;

}

</script>

</body>

</html>

以上就是对移动端图片无缝滚动是如何实现的?(代码)的全部介绍,如果您想了解更多有关HTML视频教程,请关注PHP中文网。

以上就是移动端图片无缝滚动是如何实现的?(代码)的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

移动端图片无缝滚动是如何实现的?(代码)

css flex 布局实现无缝滚动

更多相关阅读请进入《移动端图片》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...