Home Web Front-end JS Tutorial JS realizes the layer effect code that scrolls with the scroll bar on the web page_javascript skills

JS realizes the layer effect code that scrolls with the scroll bar on the web page_javascript skills

May 16, 2016 pm 03:33 PM
js Web page

The example in this article describes the JS implementation of the layer effect code that scrolls with the scroll bar on the web page. Share it with everyone for your reference, the details are as follows:

The scrolling layer code on this webpage follows the scroll bar. Drag the scroll bar to see the effect. The code for the couplet advertisement is also based on this. The floating layer on the right can also be closed with a little processing. Implement the code for a floating ad, run it and see the effect first.

The screenshot of the running effect is as follows:

The online demo address is as follows:

http://demo.jb51.net/js/2015/js-web-fixed-scroll-adv-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

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

<!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>

body{ margin:0; padding:0; font-size:12px; font-family:"宋体",Arial, Helvetica, sans-serif;}

div,p,ul,ol,li,dl,dt,dd,h1,h2,h3,h4,h5,h6,form,input,form{ margin:0; padding:0;}

h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal;}

img{ border:0;}

ol,ul{list-style:none;}

a{ text-decoration:none; color:#fff;}

a:hover{ text-decoration:none;}

#scroll_div{ width:100px; height:400px; background:#990; }

#btn_close,#btn_gotop{ cursor:pointer;}

</style>

</head>

<body>

<div style=" width:1002px;height:42px;margin:0 auto; background-color:#060;">头</div>

<div style="width:1002px; margin:0 auto; background-color:#f60;">

我们提供各类编程源码、<br>素材、书籍教程、设计模板、<br>网页特效代码以及常用软件下载等,<br>做有质量的<br>学习型源码下载站。

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

<br>

</div>

<div style=" width:1002px;height:95px;margin:0 auto; background-color:#060;">底</div>

<div id="scroll_div">

<span id="btn_close">关闭</span><br />

<span id="btn_gotop">返回顶部</span>

</div>

<script type="text/javascript">

var Bianyuan = {

 //添加事件2(DOM-保证this指向对象是obj)

 addEvent : function(obj, type, fn){

  if (obj.attachEvent){

   obj['e'+type+fn] = fn;

   obj[type+fn] = function(){obj['e'+type+fn](window.event);}

   obj.attachEvent('on'+type, obj[type+fn]);

  }else{

   obj.addEventListener(type, fn, false);

  }

 },

 //获取id元素

 $ : function(id){

  return document.getElementById(id);

 },

 //取得浏览器可视区size

 getBrowserSize : function(){

  var pageWidth = window.innerWidth,

   pageHeight = window.innerHeight;

  if (typeof pageWidth != "number"){

   if (document.compatMode == "CSS1Compat"){

    pageWidth = document.documentElement.clientWidth;

    pageHeight = document.documentElement.clientHeight;

   } else{

    pageWidth = document.body.clientWidth;

    pageWidth = document.body.clientHeight;

   }

  }

  //ie减去17滚动条宽度

  if(!window.ActiveXObject){

   pageWidth -= 17;

  }

  return {

   width : pageWidth,

   height : pageHeight

  }

 },

 //获取滚动条高度

 getPageScroll : function(){

  var yScroll;

  if (self.pageYOffset) {

   yScroll = self.pageYOffset;

  } else if (document.documentElement && document.documentElement.scrollTop){

   yScroll = document.documentElement.scrollTop;

  } else if (document.body) {

   yScroll = document.body.scrollTop;

  }

  return yScroll;

 }

}

function scrollBar(scrollId, dir, headHeight, footHeight, areaWidth, o){

 //滚动框宽高

 var scrollBar = Bianyuan.$(scrollId),

  barWidth = scrollBar.offsetWidth,

  barHeight = scrollBar.offsetHeight;

 //可视区宽高

 var pageWidth = Bianyuan.getBrowserSize().width,

  pageHeight = Bianyuan.getBrowserSize().height;

 //内容宽高

 var widthMore = document.documentElement.scrollWidth,

  heightMore = document.body.scrollHeight||(document.documentElement.scrollHeight); 

 //最大宽高

 var maxWidth = Math.max(pageWidth, widthMore),

  maxHeight = Math.max(pageHeight, heightMore);

 scrollBar.style.position = 'absolute';

 //设置top--假设滚动框高度小于整个页面高度,如果大于直接将其隐藏

 if (maxHeight < (barHeight + footHeight + footHeight + 4)){

  scrollBar.style.display = 'none';

 }else{

  //不挡顶部---如果想改距顶高度,改下边的第一个2值

  scrollBar.style.top = Bianyuan.getPageScroll() + headHeight + 2 + 'px';

  //不挡底部

  if ((heightMore - Bianyuan.getPageScroll() - barHeight) <= footHeight){

   scrollBar.style.top = heightMore - footHeight - barHeight - 2 + 'px';

  }

 }

 //无视主体内容-左右

 if (dir == 'left' && areaWidth == 1){

  scrollBar.style.left = 2 + 'px';

 }else if (dir == 'right' && areaWidth == 1){

  scrollBar.style.left = maxWidth - barWidth - 2 + 'px';

 //根据主体内容宽-左右

 }else if (dir == 'left' && areaWidth != 1){

  if (pageWidth >= (barWidth*2 + areaWidth + 4)){

   scrollBar.style.left = (pageWidth - areaWidth)/2 - barWidth - 2 + 'px';

  }else{

   scrollBar.style.left = 2 + 'px';

  }

 }else if (dir == 'right' && areaWidth != 1){

  if (pageWidth >= (barWidth*2 + areaWidth + 4)){

   scrollBar.style.left = (pageWidth - areaWidth)/2 + areaWidth + 2 + 'px';

  }else{

   scrollBar.style.left = maxWidth - barWidth - 2 + 'px';

  }

 }

 //关闭和返回顶部

 if (o){

  if (o.btnClose){

   var closeBtn = Bianyuan.$(o.btnClose);

   Bianyuan.addEvent(closeBtn, 'click', function(){

    scrollBar.style.display = 'none';

   })

  }

  if (o.goTop){

   var gotopBtn = Bianyuan.$(o.goTop);

   Bianyuan.addEvent(gotopBtn, 'click', function(){

    document.documentElement.scrollTop = 0;

    document.body.scrollTop = 0;

   })

  }

 }

 //改变窗口大小或滚动条滚动

 resizeWindow(scrollId, dir, headHeight, footHeight, areaWidth, o);

}

function resizeWindow(scrollId, dir, headHeight, footHeight, areaWidth, o){

 window.onresize = function(){scrollBar(scrollId, dir, headHeight, footHeight, areaWidth, o);}

 window.onscroll = function(){scrollBar(scrollId, dir, headHeight, footHeight, areaWidth, o);}

}

</script>

<script type="text/javascript">

scrollBar("scroll_div", "right", 42, 95, 1002, {btnClose:"btn_close",goTop:"btn_gotop"});

</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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 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 send web pages to desktop as shortcut in Edge browser? How to send web pages to desktop as shortcut in Edge browser? Mar 14, 2024 pm 05:22 PM

How to send web pages to the desktop as a shortcut in Edge browser? Many of our users want to display frequently used web pages on the desktop as shortcuts for the convenience of directly opening access pages, but they don’t know how to do it. In response to this problem, the editor of this issue will share the solution with the majority of users. , let’s take a look at the content shared in today’s software tutorial. The shortcut method of sending web pages to the desktop in Edge browser: 1. Open the software and click the "..." button on the page. 2. Select "Install this site as an application" in "Application" from the drop-down menu option. 3. Finally, click it in the pop-up window

Possible reasons why the network connection is normal but the browser cannot access the web page Possible reasons why the network connection is normal but the browser cannot access the web page Feb 19, 2024 pm 03:45 PM

The browser cannot open the web page but the network is normal. There are many possible reasons. When this problem occurs, we need to investigate step by step to determine the specific cause and solve the problem. First, determine whether the webpage cannot be opened is limited to a specific browser or whether all browsers cannot open the webpage. If only one browser cannot open the web page, you can try to use other browsers, such as Google Chrome, Firefox, etc., for testing. If other browsers are able to open the page correctly, the problem is most likely with that specific browser, possibly

What should I do if the images on the webpage cannot be loaded? 6 solutions What should I do if the images on the webpage cannot be loaded? 6 solutions Mar 15, 2024 am 10:30 AM

Some netizens found that when they opened the browser web page, the pictures on the web page could not be loaded for a long time. What happened? I checked that the network is normal, so where is the problem? The editor below will introduce to you six solutions to the problem that web page images cannot be loaded. Web page images cannot be loaded: 1. Internet speed problem The web page cannot display images. It may be because the computer's Internet speed is relatively slow and there are more softwares opened on the computer. And the images we access are relatively large, which may be due to loading timeout. As a result, the picture cannot be displayed. You can turn off the software that consumes more network speed. You can go to the task manager to check. 2. Too many visitors. If the webpage cannot display pictures, it may be because the webpages we visited were visited at the same time.

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

What to do if the webpage cannot be opened What to do if the webpage cannot be opened Feb 21, 2024 am 10:24 AM

How to solve the problem of web pages not opening With the rapid development of the Internet, people increasingly rely on the Internet to obtain information, communicate and entertain. However, sometimes we encounter the problem that the web page cannot be opened, which brings us a lot of trouble. This article will introduce you to some common methods to help solve the problem of web pages not opening. First, we need to determine why the web page cannot be opened. Possible reasons include network problems, server problems, browser settings problems, etc. Here are some solutions: Check network connection: First, we need

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 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

How to open php on the web page How to open php on the web page Mar 22, 2024 pm 03:20 PM

Executing PHP code in a web page requires ensuring that the web server supports PHP and is properly configured. PHP can be opened in three ways: * **Server environment:** Place the PHP file in the server root directory and access it through the browser. * **Integrated Development Environment: **Place PHP files in the specified web root directory and access them through the browser. * **Remote Server:** Access PHP files hosted on a remote server via the URL address provided by the server.

See all articles