当前位置:网站首页>盒子移动和滚动加载效果练习
盒子移动和滚动加载效果练习
2022-08-02 03:35:00 【呦呦鹿鸣[email protected]】
针对 offset、client 和 scroll 的加强练习
盒子移动
<div class="box box1"></div>
<div class="box box2"></div>
<script>
var box1=document.querySelector('.box1');
var box2=document.querySelector('.box2');
//匀速运动
function move(obj,target){
obj.myInter=setInterval(function(){
var offsetLeft=obj.offsetLeft;
//console.log(offsetLeft);
var num=10; //每次移动10个像素
//var target=100;
if(offsetLeft==target){
//清除定时器
clearInterval(obj.mtInter);
}else{
obj.style.left=offsetLeft+num+'px';
}
},1000)
}
box1.onclick=function(){
move(this,100);
}
//缓动运动 有个滑动效果 每次移动的距离 由大到小改变
//思路 要移动100 可以先走40 再走30 再走20 再走10 这样移动就会越来越慢
function slow(obj,target){
obj.myInter=setInterval(function(){
var offsetLeft=obj.offsetLeft;
var num=(target-offsetLeft)/10;
//Math.ceil向上取整 Math.floor向下取整
num>0?num=Math.ceil(num):num=Math.floor(num);
//注意 随着定时器的运行 num数值会由大到小发生变化
//因为offsetLeft的值在变大
if(offsetLeft==target){
clearInterval(obj.myInter);
}else{
obj.style.left=offsetLeft+num+'px';
}
},1000);
}
box2.onclick=function(){
slow(this,100);
}
</script>
滚动加载效果
滑动滚动条,判断滚动条是否到达底部,是到达底部后继续加载新内容继续滚动
<div class="box">
<p>我是内容<br>我是内容<br>我是内容<br>我是内容<br>我是内容<br>我是内容<br></p>
<p>我是内容<br>我是内容<br>我是内容<br>我是内容<br>我是内容<br>我是内容<br></p>
<p>我是内容<br>我是内容<br>我是内容<br>我是内容<br>我是内容<br>我是内容<br></p>
<p>我是内容<br>我是内容<br>我是内容<br>我是内容<br>我是内容<br>我是内容<br></p>
<p>我是内容<br>我是内容<br>我是内容<br>我是内容<br>我是内容<br>我是内容<br></p>
</div>
<script type="text/javascript">
var box = document.querySelector('.box');
box.addEventListener('scroll',function(){
// console.log('滚动了');
var scrollTop = box.scrollTop;//获得向上滚动的距离
// console.log(scrollTop);
var clientHeight = box.clientHeight;
var scrollHeight = box.scrollHeight;
console.log(scrollTop,clientHeight,scrollHeight);
// 如果出现小数 建议做四舍五入
// Math.round(scrollTop+clientHeight)
if(Math.round(scrollTop+clientHeight)>=scrollHeight){
// console.log('到底了');
//创建p标签
var pNode = document.createElement('p');
pNode.innerHTML = '新添加的内容';
//尾部插入标签
box.appendChild(pNode);
}
})
</script>
原网站
版权声明
本文为[呦呦鹿鸣[email protected]]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_53841687/article/details/125625839
边栏推荐
猜你喜欢
随机推荐
【LeetCode】链表相加 进位
QT中更换OPENCV版本(3->4),以及一些宏定义的改变
webdriver封装
最长公共子串
学习(三):事件的订阅与发布
侦听器watch及其和计算属性、methods方法的总结
The slave I/O thread stops because master and slave have equal MySQL server ids
想低成本保障软件安全?5大安全任务值得考虑
工作过程中问题汇总
Mysql数据库入门 (基础知识点 由来 各种指令 如何运用)
滑动窗口方法
jni中jstring与char*互转
字符串哈希
【LeetCode】合并
shell脚本的基础知识
QT之实现斗鱼直播PC客户端
Mongoose无法更新时间戳
onvif/rtsp转gb28181协议,无缝对接国标平台
谷粒商城10——搜索、商品详情、异步编排
h264转hls