Home Web Front-end JS Tutorial Page custom scroll bar effect implemented by JS_javascript skills

Page custom scroll bar effect implemented by JS_javascript skills

May 16, 2016 pm 03:35 PM
js scroll bar customize page

The example in this article describes the page custom scroll bar effect implemented by JS. Share it with everyone for your reference, the details are as follows:

Here is a demonstration of the scroll bar effect used on a web page. It is a customized scroll bar code. Except for the up and down arrows, the scroll bar is basically the same as a normal browser. The mouse wheel scrolls and the scroll bar scrolls. The html structure is very simple, mainBox is the outer div, content is the content, and the scroll bar div is dynamically generated by js.

The screenshot of the running effect is as follows:

The online demo address is as follows:

http://demo.jb51.net/js/2015/js-web-zdy-scroll-style-codes/

The specific code is as follows:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

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

<title>滚动条</title>

<style type="text/css">

*{ margin:0; padding:0;}

p{ height:1000px;}

#mainBox{ width:400px; height:500px; border:1px #bbb solid; position:relative; overflow:hidden; margin:50px auto;}

#content{ height:2500px; position:absolute; left:0; top:0; background:url(http://files.jb51.net/file_images/article/201510/20151026113716032.jpg) }

.scrollDiv{ width:18px; position:absolute; top:0; background:#666; border-radius:10px;}

</style>

</head>

<body>

<div id="mainBox">

 <div id="content"></div>

</div>

<p></p>

<script type="text/javascript">

var doc=document;

var _wheelData=-1;

var mainBox=doc.getElementById('mainBox');

function bind(obj,type,handler){

 var node=typeof obj=="string"&#63;$(obj):obj;

 if(node.addEventListener){

  node.addEventListener(type,handler,false);

 }else if(node.attachEvent){

  node.attachEvent('on'+type,handler);

 }else{

  node['on'+type]=handler;

 }

}

function mouseWheel(obj,handler){

 var node=typeof obj=="string"&#63;$(obj):obj;

  bind(node,'mousewheel',function(event){

   var data=-getWheelData(event);

   handler(data);

   if(document.all){

    window.event.returnValue=false;

   }else{

    event.preventDefault();

   }

  });

  //火狐

  bind(node,'DOMMouseScroll',function(event){

   var data=getWheelData(event);

   handler(data);

   event.preventDefault();

  });

  function getWheelData(event){

   var e=event||window.event;

   return e.wheelDelta&#63;e.wheelDelta:e.detail*40;

  }

}

function addScroll(){

 this.init.apply(this,arguments);

}

addScroll.prototype={

 init:function(mainBox,contentBox,className){

  var mainBox=doc.getElementById(mainBox);

  var contentBox=doc.getElementById(contentBox);

  var scrollDiv=this._createScroll(mainBox,className);

  this._resizeScorll(scrollDiv,mainBox,contentBox);

  this._tragScroll(scrollDiv,mainBox,contentBox);

  this._wheelChange(scrollDiv,mainBox,contentBox);

  this._clickScroll(scrollDiv,mainBox,contentBox);

 },

 //创建滚动条

 _createScroll:function(mainBox,className){

  var _scrollBox=doc.createElement('div')

  var _scroll=doc.createElement('div');

  var span=doc.createElement('span');

  _scrollBox.appendChild(_scroll);

  _scroll.appendChild(span);

  _scroll.className=className;

  mainBox.appendChild(_scrollBox);

  return _scroll;

 },

 //调整滚动条

 _resizeScorll:function(element,mainBox,contentBox){

  var p=element.parentNode;

  var conHeight=contentBox.offsetHeight;

  var _width=mainBox.clientWidth;

  var _height=mainBox.clientHeight;

  var _scrollWidth=element.offsetWidth;

  var _left=_width-_scrollWidth;

  p.style.width=_scrollWidth+"px";

  p.style.height=_height+"px";

  p.style.left=_left+"px";

  p.style.position="absolute";

  p.style.background="#ccc";

  contentBox.style.width=(mainBox.offsetWidth-_scrollWidth)+"px";

  var _scrollHeight=parseInt(_height*(_height/conHeight));

  if(_scrollHeight>=mainBox.clientHeight){

   element.parentNode.style.display="none";

  }

  element.style.height=_scrollHeight+"px";

 },

 //拖动滚动条

 _tragScroll:function(element,mainBox,contentBox){

  var mainHeight=mainBox.clientHeight;

  element.onmousedown=function(event){

   var _this=this;

   var _scrollTop=element.offsetTop;

   var e=event||window.event;

   var top=e.clientY;

   //this.onmousemove=scrollGo;

   document.onmousemove=scrollGo;

   document.onmouseup=function(event){

    this.onmousemove=null;

   }

   function scrollGo(event){

    var e=event||window.event;

    var _top=e.clientY;

    var _t=_top-top+_scrollTop;

    if(_t>(mainHeight-element.offsetHeight)){

     _t=mainHeight-element.offsetHeight;

    }

    if(_t<=0){

     _t=0;

    }

    element.style.top=_t+"px";

    contentBox.style.top=-_t*(contentBox.offsetHeight/mainBox.offsetHeight)+"px";

    _wheelData=_t;

   }

  }

  element.onmouseover=function(){

   this.style.background="#444";

  }

  element.onmouseout=function(){

   this.style.background="#666";

  }

 },

 //鼠标滚轮滚动,滚动条滚动

 _wheelChange:function(element,mainBox,contentBox){

  var node=typeof mainBox=="string"&#63;$(mainBox):mainBox;

  var flag=0,rate=0,wheelFlag=0;

  if(node){

   mouseWheel(node,function(data){

    wheelFlag+=data;

    if(_wheelData>=0){

     flag=_wheelData;

     element.style.top=flag+"px";

     wheelFlag=_wheelData*12;

     _wheelData=-1;

    }else{

     flag=wheelFlag/12;

    }

    if(flag<=0){

     flag=0;

     wheelFlag=0;

    }

    if(flag>=(mainBox.offsetHeight-element.offsetHeight)){

     flag=(mainBox.clientHeight-element.offsetHeight);

     wheelFlag=(mainBox.clientHeight-element.offsetHeight)*12;

    }

    element.style.top=flag+"px";

    contentBox.style.top=-flag*(contentBox.offsetHeight/mainBox.offsetHeight)+"px";

   });

  }

 },

 _clickScroll:function(element,mainBox,contentBox){

  var p=element.parentNode;

  p.onclick=function(event){

   var e=event||window.event;

   var t=e.target||e.srcElement;

   var sTop=document.documentElement.scrollTop>0&#63;document.documentElement.scrollTop:document.body.scrollTop;

   var top=mainBox.offsetTop;

   var _top=e.clientY+sTop-top-element.offsetHeight/2;

   if(_top<=0){

    _top=0;

   }

   if(_top>=(mainBox.clientHeight-element.offsetHeight)){

    _top=mainBox.clientHeight-element.offsetHeight;

   }

   if(t!=element){

    element.style.top=_top+"px";

    contentBox.style.top=-_top*(contentBox.offsetHeight/mainBox.offsetHeight)+"px";

    _wheelData=_top;

   }

  }

 }

}

new addScroll('mainBox','content','scrollDiv');

</script>

</body>

</html>

Copy after login

I hope this article will be helpful to everyone in JavaScript programming.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to copy a page in Word How to copy a page in Word Feb 20, 2024 am 10:09 AM

Want to copy a page in Microsoft Word and keep the formatting intact? This is a smart idea because duplicating pages in Word can be a useful time-saving technique when you want to create multiple copies of a specific document layout or format. This guide will walk you through the step-by-step process of copying pages in Word, whether you are creating a template or copying a specific page in a document. These simple instructions are designed to help you easily recreate your page without having to start from scratch. Why copy pages in Microsoft Word? There are several reasons why copying pages in Word is very beneficial: When you have a document with a specific layout or format that you want to copy. Unlike recreating the entire page from scratch

How to quickly set up a custom avatar in Netflix How to quickly set up a custom avatar in Netflix Feb 19, 2024 pm 06:33 PM

An avatar on Netflix is ​​a visual representation of your streaming identity. Users can go beyond the default avatar to express their personality. Continue reading this article to learn how to set a custom profile picture in the Netflix app. How to quickly set a custom avatar in Netflix In Netflix, there is no built-in feature to set a profile picture. However, you can do this by installing the Netflix extension on your browser. First, install a custom profile picture for the Netflix extension on your browser. You can buy it in the Chrome store. After installing the extension, open Netflix on your browser and log into your account. Navigate to your profile in the upper right corner and click

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to customize shortcut key settings in Eclipse How to customize shortcut key settings in Eclipse Jan 28, 2024 am 10:01 AM

How to customize shortcut key settings in Eclipse? As a developer, mastering shortcut keys is one of the keys to improving efficiency when coding in Eclipse. As a powerful integrated development environment, Eclipse not only provides many default shortcut keys, but also allows users to customize them according to their own preferences. This article will introduce how to customize shortcut key settings in Eclipse and give specific code examples. Open Eclipse First, open Eclipse and enter

The operation process of edius custom screen layout The operation process of edius custom screen layout Mar 27, 2024 pm 06:50 PM

1. The picture below is the default screen layout of edius. The default EDIUS window layout is a horizontal layout. Therefore, in a single-monitor environment, many windows overlap and the preview window is in single-window mode. 2. You can enable [Dual Window Mode] through the [View] menu bar to make the preview window display the playback window and recording window at the same time. 3. You can restore the default screen layout through [View menu bar>Window Layout>General]. In addition, you can also customize the layout that suits you and save it as a commonly used screen layout: drag the window to a layout that suits you, then click [View > Window Layout > Save Current Layout > New], and in the pop-up [Save Current Layout] Layout] enter the layout name in the small window and click OK

How to deal with the problem that Laravel page cannot display CSS correctly How to deal with the problem that Laravel page cannot display CSS correctly Mar 10, 2024 am 11:33 AM

"Methods to handle Laravel pages that cannot display CSS correctly, need specific code examples" When using the Laravel framework to develop web applications, sometimes you will encounter the problem that the page cannot display CSS styles correctly, which may cause the page to render abnormal styles. Affect user experience. This article will introduce some methods to deal with the failure of Laravel pages to display CSS correctly, and provide specific code examples to help developers solve this common problem. 1. Check the file path. First check the path of the CSS file.

How to create a stock candlestick chart using PHP and JS How to create a stock candlestick chart using PHP and JS Dec 17, 2023 am 08:08 AM

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

See all articles