Javascript-Based Seamless text scrolling

Source: Internet
Author: User

Javascript-Based Seamless text scrolling

This article mainly shares with you the code that uses pure javascript to implement a controllable. The effect is very good. If you need it, you can refer to it.

Seamless text scrolling:

?

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

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

<! DOCTYPE html>

<! -- [If lt IE 7]>

<! -- [If IE 7]>

<! -- [If IE 8]>

<! -- [If IE 9]>

<! -- [If (gt IE 9) |! (IE)]> <! --> <Html lang = "zh-CN"> <! -- <! [Endif] -->

<Head>

<Title> text scrolling </title>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

 

<Style type = "text/css">

* {Margin: 0; padding: 0 ;}

Body {padding: 20px ;}

. Textbox {border: 1px solid # ddd; width: auto; overflow: hidden ;}

. Textbox ul {list-style: none; position: relative ;}

. Textbox ul li {padding: 5px 0 ;}

</Style>

</Head>

<Body class = "home-page">

 

<Div id = "textbox" class = "textbox">

<Ul>

<Li> automobile | Sports Class B cars drop by 30 thousand 5 </li>

<Li> Home | a woman with such great decoration really cannot sit down </li>

<Li> Education | RMB 0.1 million/person for the China (Shanghai) University of California (Shanghai) </li>

<Li> automobile | Sports Class B cars drop by 30 thousand 5 parallel imported cars increase by 0.11 million </li>

<Li> healthy | 14-year-old boy suffering from Hormone Abuse cannot grow for 10 years </li>

<Li> digital | what is the latest mobile phone price to say about the acceleration of broadband fee reduction? </Li>

<Li> auto | additional price of parallel imported vehicles: 0.11 million </li>

<Li> automobile | Sports Class B cars drop by 30 thousand 5 </li>

<Li> auto | additional price of parallel imported vehicles: 0.11 million </li>

<Li> sports | Evergrande AFC Champions League wins genuine jerseys by guessing scores </li>

</Ul>

<A href = "#" class = "btnPrev"> left </a>

<A href = "#" class = "btnNext"> right </a>

</Div>

<Br>

<Br>

<Div id = "textbox2" class = "textbox">

<Ul>

<Li> automobile | Sports Class B cars drop by 30 thousand 5 </li>

<Li> Home | a woman with such great decoration really cannot sit down </li>

<Li> Education | RMB 0.1 million/person for the China (Shanghai) University of California (Shanghai) </li>

<Li> automobile | Sports Class B cars drop by 30 thousand 5 parallel imported cars increase by 0.11 million </li>

<Li> healthy | 14-year-old boy suffering from Hormone Abuse cannot grow for 10 years </li>

<Li> digital | what is the latest mobile phone price to say about the acceleration of broadband fee reduction? </Li>

<Li> auto | additional price of parallel imported vehicles: 0.11 million </li>

<Li> automobile | Sports Class B cars drop by 30 thousand 5 </li>

<Li> auto | additional price of parallel imported vehicles: 0.11 million </li>

<Li> sports | Evergrande AFC Champions League wins genuine jerseys by guessing scores </li>

</Ul>

<A href = "#" class = "btnPrev"> up </a>

<A href = "#" class = "btnNext"> down </a>

</Div>

<Script type = "text/javascript" src = "scripts/jquery-1.11.1.min.js"> </script>

<Script type = "text/javascript">

 

// Scrolled seamlessly from the left-side Navigation Pane

Scroll ('# textbox', {vis: 2, btnHidden: false, dir: 'prev', type: 'H '});

Scroll ('# textbox2', {vis: 3, btnHidden: false, dir: 'prev', type: 'V '});

 

Function scroll (container, options ){

Var box = $ (container );

Var boxUl = box. find ('ul '). eq (0 );

Var LiHeight = 0; // The height of the cloned li list is not included.

Var cloneLiHeight = 0; // contain the height of the cloned li list

Var LiWidth = 0; // The width of the cloned li list is not included.

Var cloneLiWidth = 0; // contains the width of the cloned li list

Var Lis = boxUl. children ();

Var btnPrev = box. find ('. btnprev ');

Var btnNext = box. find ('. btnNext ');

 

// Default parameter

Var defult = {

Vis: 2, // display number

AutoPlay: true, // automatic playback

Speed: 50, // scroll speed

Dir: 'prev', // scroll direction

BtnHidden: false, // whether the button is hidden

Type: 'V' // horizontal or vertical

 

};

Var opt = $. extend ({}, defult, options );

 

 

// Construct the DOM Structure

Var lastClone = 0; // The last cloned Element

Var lastCloneHeight = 0; // The height of the last cloned Element

Var allCloneHeight = 0; // total height of all cloned Elements

Var lastCloneWidth = 0;

Var allCloneWidth = 0;

Var visHeight = 0; // visible height

Var visWidth = 0;

Var boxUl_wrap;

 

If (opt. type = "v") {// vertical direction

Lis. each (function (){

$ (This). height ($ (this). height ());

LiHeight + = $ (this). outerHeight (true );

});

LastClone = boxUl. children (': last'). clone (). addClass ('clone'). prependTo (boxUl );

LastCloneHeight = lastClone. outerHeight (true );

AllCloneHeight = lastClone. outerHeight (true );

 

For (var I = 0; I <opt. vis; I ++ ){

Lis. eq (I). clone (). addClass ('clone '). appendTo (boxUl );

AllCloneHeight + = Lis. eq (I). outerHeight (true );

}

 

VisHeight = allCloneHeight-lastCloneHeight;

CloneLiHeight = LiHeight + allCloneHeight;

 

BoxUl_wrap = $ ('<div> </div> 'privacy .css ({'width': '000000', 'height': visHeight, 'overflow': 'hidd ', 'position': 'relative '}). attr ('id', 'ulwre ');

BoxUl.css ({'height': cloneLiHeight, 'top':-lastCloneHeight}). wrap (boxUl_wrap );

 

} Else if (opt. type = "h") {// horizontal direction

Lis.css ({'white': 'nowre', 'float': 'left', 'paddingright': '10px '});

Lis. each (function (){

$ (This). width ($ (this). width ());

LiWidth + = $ (this). outerWidth (true );

});

 

LastClone = boxUl. children (': last'). clone (). addClass ('clone'). prependTo (boxUl );

LastCloneWidth = lastClone. outerWidth (true );

AllCloneWidth = lastClone. outerWidth (true );

 

For (var j = 0; j <opt. vis; j ++ ){

Lis. eq (j). clone (). addClass ('clone '). appendTo (boxUl );

AllCloneWidth + = Lis. eq (j). outerWidth (true );

}

VisHeight = Lis. eq (0). outerHeight (true );

VisWidth = allCloneWidth-lastCloneWidth;

CloneLiWidth = LiWidth + allCloneWidth;

 

BoxUl_wrap = $ ('<div> </div> 'privacy .css ({'width': visWidth, 'height': visHeight, 'overflow': 'hidd', 'position ': 'relative '}). attr ('id', 'ulwre ');

BoxUl.css ({'width': cloneLiWidth, 'left':-lastCloneWidth}). wrap (boxUl_wrap );

Box.css ({'width': visWidth });

}

 

 

// Add event processing

Var timer = null;

Var scrollTop = function (){

ClearInterval (timer );

Timer = setInterval (function (){

Var tmp = parseInt(boxUl.css ('top'). replace ('px ',""));

Tmp --;

BoxUl. animate ({'top': tmp}, 0, function (){

If (tmp <=-(LiHeight + lastCloneHeight )){

BoxUl.css ('top',-lastCloneHeight );

}

});

}, Opt. speed );

};

 

Var scrollDown = function (){

ClearInterval (timer );

Timer = setInterval (function (){

Var tmp = parseInt(boxUl.css ('top'). replace ('px ',""));

Tmp ++;

BoxUl. animate ({'top': tmp}, 0, function (){

If (tmp> = 0 ){

BoxUl.css ('top',-(LiHeight ));

}

});

}, Opt. speed );

};

 

Var scrollLeft = function (){

ClearInterval (timer );

Timer = setInterval (function (){

Var tmp = parseInt(boxUl.css ('left'). replace ('px ',""));

Tmp --;

BoxUl. animate ({'left': tmp}, 0, function (){

If (tmp <=-(LiWidth + lastCloneWidth )){

BoxUl.css ('left',-(lastCloneWidth ));

}

});

}, Opt. speed );

};

 

Var scrollRight = function (){

ClearInterval (timer );

Timer = setInterval (function (){

Var tmp = parseInt(boxUl.css ('left'). replace ('px ',""));

Tmp ++;

BoxUl. animate ({'left': tmp}, 0, function (){

If (tmp> = 0 ){

BoxUl.css ('left',-(LiWidth ));

}

});

}, Opt. speed );

};

 

Var scrollType = function (type, dir ){

If (Lis. length> = opt. vis) {// The number of displays cannot exceed the number of lists; otherwise, the results are not displayed.

Var sdir = typeof dir! = "Undefined "? Dir: opt. dir;

Switch (type ){

Case "v ":

If (sdir = "prev") {scrollTop () ;}else {scrollDown ();}

Break;

Case "h ":

If (sdir = "prev") {scrollLeft () ;}else {scrollRight ();}

}

}

};

 

 

If (opt. autoPlay ){

ScrollType (opt. type );

}

 

// Add event processing

Box. find ('# ulWrap'). hover (function (){

ClearInterval (timer );

}, Function (){

ScrollType (opt. type );

});

 

// Hide the button

If (! Opt. btnHidden ){

 

BtnPrev. unbind ('mouseover ');

BtnNext. unbind ('mouseover ');

 

BtnPrev. mouseover (function (){

ScrollType (opt. type, "prev ");

});

BtnNext. mouseover (function (){

ScrollType (opt. type, "next ");

});

} Else {

BtnPrev. hide ();

BtnNext. hide ();

}

 

}

</Script>

</Body>

</Html>

Some questions:

There is no problem with local hosts, But it passes through document. after the write () operator is written into the primary row, the li height () operator in the vertical mode has a problem. The reason is unknown ..

 

The above is all the content of this article. I hope you will like it.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.