js等待加载转圈圈效果



首先引入js文件,jquery.js和spin.js

css文件如下:

 <style type="text/css">
       
        #foo {
            position: fixed;
            left: 0;
            top: 0;
            _position: absolute;
            width: 100%;
            background: #000;
            opacity: 0.5;
            filter: alpha(opacity=50);
            z-index: 999;
            display: none;
        }

   
    </style>

js代码如下:

 var opts = {
            lines: 9, // The number of lines to draw
            length: 0, // The length of each line
            width: 10, // The line thickness
            radius: 15, // The radius of the inner circle
            corners: 1, // Corner roundness (0..1)
            rotate: 0, // The rotation offset
            color: '#fff', // #rgb or #rrggbb
            speed: 1, // Rounds per second
            trail: 60, // Afterglow percentage
            shadow: false, // Whether to render a shadow
            hwaccel: false, // Whether to use hardware acceleration
            className: 'spin', // The CSS class to assign to the spinner
            zIndex: 2e9, // The z-index (defaults to 2000000000)
            top: '50%', // Top position relative to parent in px
            left: '50%' // Left position relative to parent in px
        };
        var spinner = new Spinner(opts);
        //显示与隐藏加载动画
        function showLoading(result) {
            var spinContainer = document.getElementById("foo");
            if (result) {
                var target = $(spinContainer).get(0);
                spinner.spin(target);
                spinContainer.style.height = document.documentElement.clientHeight + "px";
                $(spinContainer).show();
            } else {
                spinner.spin();
                $(spinContainer).hide();
            }
        }

 在body标签下放入如下div:

<div id="foo" style="width: 100%; height: 100%"></div>

需要显示转圈圈直接调用 showLoading(true);取消转圈圈直接调用showLoading(false);

在JavaScript中创建一个转圈圈加载动画通常涉及到HTML、CSS和JavaScript的结合使用。下面是一个简单的例子: 首先,在HTML中创建一个用于显示加载动画的元素: ```html <div id="loadingSpinner"></div> ``` 然后,使用CSS来设置这个元素的基本样式以及动画效果: ```css #loadingSpinner { border: 16px solid #f3f3f3; /* 轻灰色背景 */ border-top: 16px solid #3498db; /* 蓝色加载线条 */ border-radius: 50%; /* 边框圆形 */ width: 120px; height: 120px; animation: spin 2s linear infinite; /* 旋转动画 */ } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } ``` 最后,使用JavaScript来控制加载动画的显示与隐藏: ```javascript // 显示加载动画 function showLoadingSpinner() { document.getElementById('loadingSpinner').style.display = 'block'; } // 隐藏加载动画 function hideLoadingSpinner() { document.getElementById('loadingSpinner').style.display = 'none'; } // 调用显示加载动画的函数 showLoadingSpinner(); // 假设在某个操作完成后隐藏加载动画 setTimeout(hideLoadingSpinner, 2000); // 2秒后自动隐藏加载动画 ``` 这个例子中,我们首先定义了一个旋转动画`spin`,然后在HTML中有一个用于显示加载动画的`div`元素。通过JavaScript的`showLoadingSpinner`函数来显示这个动画,然后在特定的操作完成后(例如页面数据加载完成),使用`hideLoadingSpinner`函数来隐藏它。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值