web页面滚动动画效果(性能方面)

一、页面滚动动画

1、wow.js

2、可视区域插入动效vue3 Composition API

https://www.cnblogs.com/xiaonian8/p/15065727.html

二、元素是否在视窗内的判断

1、第一种方案——传统计算

function isInViewPort(element) {
  const viewWidth = window.innerWidth || document.documentElement.clientWidth;
  const viewHeight = window.innerHeight || document.documentElement.clientHeight;
  const {
    top,
    right,
    bottom,
    left,
  } = element.getBoundingClientRect();
 
  return (
    top >= 0 &&
    left >= 0 &&
    right <= viewWidth &&
    bottom <= viewHeight
  );
}
 
// usage
const.log(document.querySelector('.target')) // true or false 

2、第二种方案——Intersection Observer API 注册回调

使用教程

DEMO

三、回调执行的频率

四、页面内容高度的初始化填充

五、怎么保证页面图片显示不白屏

1、预加载

2、骨架屏方法

六、瀑布流

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>text-shadow-文字描边</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        
        .box {
            position: relative;
            margin: auto;
            transition: all .5s ease-out;
        }
        
        .box div {
            float: left;
            text-align: center;
            transition: all .5s ease-out;
            margin: 10px;
        }
        
        .w1 {
            width: 100px;
            height: 240px;
            background: #f44336;
        }
        
        .w2 {
            width: 100px;
            height: 100px;
            background: #2196f3;
        }
        
        .w3 {
            width: 100px;
            height: 300px;
            background: #ffc107;
        }
        
        .w4 {
            width: 100px;
            height: 400px;
            background: #009688;
        }
        
        .w5 {
            width: 100px;
            height: 70px;
            background: #ff9800;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="w1"></div>
        <div class="w2"></div>
        <div class="w3"></div>
        <div class="w4"></div>
        <div class="w5"></div>
        <div class="w1"></div>
        <div class="w2"></div>
        <div class="w3"></div>
        <div class="w4"></div>
        <div class="w5"></div>
        <div class="w1"></div>
        <div class="w2"></div>
        <div class="w3"></div>
        <div class="w4"></div>
        <div class="w5"></div>
    </div>
    <script>
        var boxDivWidth = 0, //元素的宽度
            boxArr = [], //存放每列的高度
            columnCount = 0; //共有几列
        boxDiv = Array.prototype.slice.call($('.box div')); //所有子元素的 数组
        //页面加载完成执行
        window.addEventListener('load', function() {
            resort();
        });
        //视口大小改变执行
        window.addEventListener('resize', function() {
            resort();
        });

        function resort() {
            var boxDiv = $('.box div');
            boxDivWidth = getWidth(boxDiv[0]); //获取单个子元素宽度
            columnCount = Math.floor((window.innerWidth - 20) / boxDivWidth); //-20 视口宽度是为了不显示水平滚动条
            $('.box')[0].style.cssText = 'width:' + boxDivWidth * columnCount + 'px;'; //计算最外层父元素 宽度 + margin 实现水平居中
            boxArr = [];
            for (var i = 0; i < columnCount; i++) { //为每列赋初值
                boxArr[i] = 0;
            }
            boxDiv.forEach(function(item) { //转换伪数组 遍历 每个子元素
                var divItemHeight = getHeight(item);
                var minValue = boxArr[0]; //高度最低列 的值
                var minIndex = 0; //高度最低列 的索引
                for (var i = 0; i < columnCount; i++) {
                    if (boxArr[i] < minValue) { //查找 高度最低的列
                        minValue = boxArr[i];
                        minIndex = i;
                    }
                }
                item.style.cssText = 'position:absolute;left:' + minIndex * boxDivWidth + 'px;top:' + minValue + 'px;'; //设置元素 left top 定位
                boxArr[minIndex] += divItemHeight; //对应列加上新增的 高度
            });
        }

        function $(domtext) {
            return document.querySelectorAll(domtext);
        }

        function getWidth(dom) {
            var style = getComputedStyle(dom); //getComputedStyle方法可以获取 外部样式表中的样式
            return parseInt(style.marginLeft.slice(0, -2)) + parseInt(style.marginRight.slice(0, -2)) + dom.offsetWidth;
        }

        function getHeight(dom) {
            var style = getComputedStyle(dom);
            return parseInt(style.marginTop.slice(0, -2)) + parseInt(style.marginBottom.slice(0, -2)) + dom.offsetHeight;
        }
    </script>
</body>

</html>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现Web页面的图形动画效果,可以使用HTML5中的canvas(画布)元素和JavaScript中的定时器。以下是一个简单的示例: HTML: ``` <canvas id="myCanvas" width="400" height="400"></canvas> ``` JavaScript: ``` // 获取画布元素和绘图上下文 var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); // 定义一个圆形对象 var circle = { x: 200, // 圆心x坐标 y: 200, // 圆心y坐标 r: 50, // 半径 color: "#ff0000" // 颜色 }; // 绘制圆形 function drawCircle() { ctx.beginPath(); ctx.arc(circle.x, circle.y, circle.r, 0, Math.PI*2); ctx.fillStyle = circle.color; ctx.fill(); ctx.closePath(); } // 更新圆形位置 function updateCircle() { circle.x += 2; // 每次向右移动2个像素 } // 清除画布 function clearCanvas() { ctx.clearRect(0, 0, canvas.width, canvas.height); } // 定义动画函数 function animate() { clearCanvas(); // 清除画布 updateCircle(); // 更新圆形位置 drawCircle(); // 绘制圆形 } // 每隔10毫秒执行一次动画函数 setInterval(animate, 10); ``` 这个示例中,我们首先定义了一个圆形对象,然后定义了三个函数:`drawCircle()`绘制圆形,`updateCircle()`更新圆形位置,`clearCanvas()`清除画布。最后,我们使用`setInterval()`函数来每隔10毫秒执行一次动画函数`animate()`,该函数依次清除画布、更新圆形位置、绘制圆形,从而实现了一个简单的动画效果。 这只是一个简单的示例,实际上可以使用canvas和定时器实现各种复杂的动画效果,例如移动、旋转、缩放、渐变等。需要注意的是,在实现复杂动画效果时,需要合理使用定时器来控制动画的帧率,避免卡顿和性能问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值