javascript - 滚动更改部分 javascript
问题描述
我想创建一个页面,当用户在浏览器中使用鼠标或滚动条滚动到底部时,一个部分会发生变化,它也有 400 或任意数字。
同样,当用户滚动到顶部时,此更改将撤消。
滚动底部时
<h1>hello<h1> <!-- show 400px scroll bottom -->
<h1>my dear<h1><!-- show next 400px scroll bottom mean 800px -->
<h1>friend<h1><!-- show next 400px scroll bottom mean 1200px -->
滚动到顶部时
<h1>friend<h1><!-- show next 400px scroll top mean 1200px -->
<h1>my dear<h1><!-- show next 400px scroll top mean 800px -->
<h1>hello<h1> <!-- show 400px scroll top mean 400px -->
我的html代码是
<div class="box">
<h1>hello<h1>
<h1>my dear<h1>
<h1>friend<h1>
</div>
我只想用 javascript 来做这件事
解决方案
you can use this code
window.onscroll = function() {myFunction()};
function myFunction() {
var winScroll = document.body.scrollTop || document.documentElement.scrollTop; // this is must to minus from a OfsetTop section before this
var element = document.querySelectorAll('h1');
console.log(Math.round(winScroll));
for(var i = 0; i < element.length; i++){
var min = 500 * i;
var max = (1000 * i)-(500 * (i-1));
// console.log(Math.round(winScroll));
if(winScroll >= min && winScroll < max){
element[i].className = "show";
console.log(Math.round(winScroll));
}else{
element[i].className = 'hide';
}
}
}
推荐阅读
- swift - 如何将存储在变量中的 xml 数据解析为字符串,以获取标签中的值?
- c - 为什么我的输出有垃圾值,以及如何修复它?
- android - 如何在 react-native 中减小 Android APK 大小?
- bash - 在 bash_profile 中添加别名时遇到问题
- python - 自动为图像设置标签
- php - Mysql 使用 php 按大小获取产品
- ssh - 如何使用 RDP 和 SSH 设置鳄梨酱会话记录
- c++ - C++ 可变参数模板包扩展为多种模板类型(例如 std::shared_ptr)是否可能?
- php - parse_ini_file 返回的数据是否总是与给定文件的顺序相同?
- typescript - Discord.js 空值检查在 TypeScript 中不起作用