当前位置:网站首页>PC side web page effects (client series, scroll series, immediate function execution, sidebar effects)
PC side web page effects (client series, scroll series, immediate function execution, sidebar effects)
2022-07-28 22:39:00 【Green hedgerow bamboo maple】
client Is the client ,client The series attribute obtains the information about the visible area of the element , adopt client Related properties of series , You can dynamically get the border size of this element , Element size, etc
| client Series properties | describe |
| element.clientTop | Returns the size of the top border of the element |
| element.clientLeft | Returns the size of the left border of the element |
| element.clientWidth | The return element itself includes padding, The width of the content area , No borders , Return value without unit |
| element.clientHeight | The return element itself includes padding, Height of content area , No borders , Return value without unit |
<style>
div {
position: absolute;
width: 300px;
height: 300px;
background-color: pink;
top: 30px;
padding: 80px;
border: 5px solid red;
}
</style><body>
<div></div>
<script>
var div = document.querySelector('div');
console.log(div.clientLeft);//5
console.log(div.clientTop);//5
console.log(div.clientHeight);//300
console.log(div.clientWidth);//300
</script>
</body>Immediate execution function , Used to create a separate scope , Execute directly without calling
(function(){})()
// perhaps
(function(){}())<body>
<script>
(function () {
alert(' Immediate execution function ')
})();
// The ginseng
(function (a) {
console.log(a);
})(3);//3
// Immediately execute semicolon separation between functions
(function (a) {
console.log(a)
}(8))//8
// Immediate execution functions are all in separate scopes , There will be no variable name conflicts
</script>
</body>Element scrolling scroll series
You can dynamically get the size and rolling distance of elements
| scroll Series properties | effect |
| element.scrollTop | Returns the distance from the upper side that was rolled away , Return value without unit |
| element.scrollLeft | Return the left distance rolled away , Return value without unit |
| element.scrollWidth | Returns its actual width , No borders , Return value without unit |
| element.scrollHeight | Return to your actual height , No borders , Return value without unit |
The head of the page being rolled away
If the browser height or width is not enough to display the entire page , The scroll bar will appear automatically , When the scroll bar scrolls down , The height hidden above the page is the height rolled up , The scroll bar will trigger when scrolling onscroll event
<style>
div {
width: 200px;
height: 200px;
background-color: pink;
border: 4px solid blue;
font-size: 100px;
overflow: auto;
}
</style><body>
<div>
ggggggggggg<br />ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
</div>
<script>
var div = document.querySelector('div');
console.log(div.scrollWidth);
console.log(div.scrollHeight);
div.addEventListener('scroll', function () {
console.log(div.scrollTop);
console.log(div.scrollLeft);
})
</script>
</body>Imitate Taobao to fix the right sidebar
1. The original sidebar was absolute positioning
2. When the page scrolls to a certain position , The sidebar becomes fixed
3. The page continues to scroll , It will show back to the top
4. Pages use scrolling events scroll, Scrolling to a certain position is to judge the upper value of the page being rolled , The curled header on the page can be passed window.pageYOffset get , If it's the rolled left side window.pageXOffset
5. Be careful : The head of the element rolled away is used element.scrollTop, If the header of the page is rolled away, it is window.pageYOffset
<style>
* {
margin: 0;
padding: 0;
}
.w {
width: 1000px;
margin: 0 auto;
}
.slide-bar {
position: absolute;
top: 600px;
/* left: 1600px; */
right: 0;
width: 100px;
height: 200px;
background-color: skyblue;
}
.goBack {
display: none;
position: absolute;
bottom: 0;
width: 100px;
height: 50px;
background-color: orange;
margin-bottom: 0;
}
.header,
.banner,
.content,
.main {
/* width: 100%; */
height: 500px;
margin-top: 20px;
}
.header {
background-color: blue;
}
.banner {
background-color: pink;
}
.content {
background-color: yellow;
}
.main {
background-color: red;
}
</style> <div class="slide-bar">
<span class="goBack"> Return to the top </span>
</div>
<div class="header w"> Head </div>
<div class="banner w">banner</div>
<div class="content w"> Content </div>
<div class="main w"> The main body </div><script>
var slidebar = document.querySelector('.slide-bar');
var banner = document.querySelector('.banner');
// prevent slidebar There's a jump
var bannerTop = banner.offsetTop;
// The value that should change after the sidebar is fixed and positioned
var slidebarTop = slidebar.offsetTop - bannerTop;
var content = document.querySelector('.content');
var goBack = document.querySelector('.goBack');
var contentTop = content.offsetTop;
document.addEventListener('scroll', function () {
// console.log(window.pageYOffset); The head of the page being rolled away
// console.log(banner.offsetTop);
if (window.pageYOffset > bannerTop) {
slidebar.style.position = 'fixed';
slidebar.style.top = slidebarTop + 'px';
} else {
slidebar.style.position = 'absolute';
slidebar.style.top = '600px';
}
if (window.pageYOffset >= contentTop) {
goBack.style.display = 'block';
} else {
goBack.style.display = 'none';
}
})
</script>mouseover And mouseenter The difference between
mouseover The mouse will trigger when it passes through its own box , After the subbox, it will trigger ,mouseenter It's only triggered by its own box
<body>
<div class="father">
<div class="son"></div>
</div>
<script>
var father = document.querySelector('.father');
father.addEventListener('mouseover', function () {
console.log('aaaa');
})
father.addEventListener('mouseenter', function () {
console.log('bbbb');
})
</script>
</body>边栏推荐
- 删除容器镜像报错解决image is referenced in multiple repositories
- 105. Construct binary tree from preorder and inorder traversal sequence (medium binary tree DFS hash table binary tree)
- What is time complexity
- Winserver operation and maintenance technology stack
- XXX port is already in use
- Detection and tracking evaluation index
- 静态成员static详解
- (翻译)图技术简明历史
- [leetcode] maximum depth of binary tree
- STM32 -- program startup process
猜你喜欢

软考网络工程师

Which is the file transfer command in the basic services of the Internet

php二维数组如何删除去除第一行元素

基于Ernie-3.0 CAIL2019法研杯要素识别多标签分类任务

(翻译)图技术简明历史

Command line agent: proxychains configuration

C language to realize string reverse order arrangement

STM32 - interrupt overview (interrupt priority)

Vscode ROS configuration GDB debugging error record

winform跳转第二个窗体案例
随机推荐
Sword finger offer II 053. Medium order successor in binary search tree (medium binary search tree DFS)
软考网络工程师
Sword finger offer II 067. maximum XOR (medium prefix tree bit operation array)
CMD common commands
JS array merging, de duplication, dimensionality reduction (es6: extended operator, set)
Chrome encountered a problem when debugging the code. After modifying and saving the code in vscode, chrome did not update after refreshing
Research cup element recognition multi label classification task based on ernie-3.0 cail2019 method
fatal error: io. h: No such file or directory
Qt+ffmpeg environment construction
redis相关
Use webworker to perform background tasks
842. 排列数字
PC side web page special effects (offset series, obtain the coordinates of the mouse in the box, pop-up drag effect, magnifying glass effect)
Jianzhi offer II 062. implement prefix tree (medium design dictionary tree prefix tree string)
Sword finger offer II 056. Sum of two nodes in a binary search tree (simple binary search tree DFS hash table double pointer iterator)
JS获取当前时间(年月日时分秒)
使用PCL批量将点云.bin文件转.pcd
[CVPR 2021] cylinder3d: cylindrical asymmetric 3D convolution network for LIDAR point cloud segmentation
Solve Jupiter: the term 'Jupiter' is not recognized as the name of a cmdlet, function, script file
【转载】token令牌在登录场景使用