MUI 加载(Loading)动画效果

//扩展mui.showLoading
(function($, window) {
    //显示加载框
    $.showLoading = function(message,type) {
        if ($.os.plus && type !== 'div') {
            $.plusReady(function() {
                plus.nativeUI.showWaiting(message);
            });
        } else {
            var html = '';
            html += '<i class="mui-spinner mui-spinner-white"></i>';
            html += '<p class="text">' + (message || "数据加载中") + '</p>';
            //遮罩层
            var mask=document.getElementsByClassName("mui-show-loading-mask");
            if(mask.length==0){
                mask = document.createElement('div');
                mask.classList.add("mui-show-loading-mask");
                document.body.appendChild(mask);
                mask.addEventListener("touchmove", function(e){e.stopPropagation();e.preventDefault();});
            }else{
                mask[0].classList.remove("mui-show-loading-mask-hidden");
            }
            //加载框
            var toast=document.getElementsByClassName("mui-show-loading");
            if(toast.length==0){
                toast = document.createElement('div');
                toast.classList.add("mui-show-loading");
                toast.classList.add('loading-visible');
                document.body.appendChild(toast);
                toast.innerHTML = html;
                toast.addEventListener("touchmove", function(e){e.stopPropagation();e.preventDefault();});
            }else{
                toast[0].innerHTML = html;
                toast[0].classList.add("loading-visible");
            }
        }
    };
    //隐藏加载框
    $.hideLoading = function(callback) {
        if ($.os.plus) {
            $.plusReady(function() {
                plus.nativeUI.closeWaiting();
            });
        }
        var mask=document.getElementsByClassName("mui-show-loading-mask");
        var toast=document.getElementsByClassName("mui-show-loading");
        if(mask.length>0){
            mask[0].classList.add("mui-show-loading-mask-hidden");
        }
        if(toast.length>0){
            toast[0].classList.remove("loading-visible");
            callback && callback();
        }
    }
})(mui, window);
/*----------------mui.showLoading-start---------------*/
.mui-show-loading {
    position: fixed;
    padding: 5px;
    width: 120px;
    min-height: 120px;
    top: 45%;
    left: 50%;
    margin-left: -60px;
    background: rgba(0, 0, 0, 0.6);
    text-align: center;
    border-radius: 5px;
    color: #FFFFFF;
    visibility: hidden;
    margin: 0;
    z-index: 2000;
    -webkit-transition-duration: .2s;
    transition-duration: .2s;
    opacity: 0;
    -webkit-transform: scale(0.9) translate(-50%, -50%);
    transform: scale(0.9) translate(-50%, -50%);
    -webkit-transform-origin: 0 0;
    transform-origin: 0 0;
}
.mui-show-loading.loading-visible {
    opacity: 1;
    visibility: visible;
    -webkit-transform: scale(1) translate(-50%, -50%);
    transform: scale(1) translate(-50%, -50%);
}
.mui-show-loading .mui-spinner{
    margin-top: 24px;
    width: 36px;
    height: 36px;
}
.mui-show-loading .text {
    line-height: 1.6;
    font-family: -apple-system-font,"Helvetica Neue",sans-serif;
    font-size: 14px;
    margin: 10px 0 0;
    color: #fff;
}
.mui-show-loading-mask {
    position: fixed;
    z-index: 1000;
    top: 0;
    right: 0;
    left: 0;
    bottom: 0;
}
.mui-show-loading-mask-hidden{
    display: none !important;
}
/*----------------mui.showLoading-end---------------*/
//页面中JS调用 
//加载文字和类型,plus环境中类型为div时强制以div方式显示
mui.showLoading("正在加载..", "div");
//callback  隐藏后的回调函数 
mui.hideLoading();
//mui.hideLoading(function(){});

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 MUI 的 CircularProgress 组件来实现 loading 效果。具体实现步骤如下: 1. 导入 CircularProgress 组件: ```javascript import CircularProgress from '@mui/material/CircularProgress'; ``` 2. 在需要展示 loading 的地方,使用 CircularProgress 组件,并设置相应的 props: ```javascript <Box sx={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}> <CircularProgress size={80} /> <Typography variant="h6" sx={{ mt: 2 }}> Loading... </Typography> </Box> ``` 其中,设置了 CircularProgress 的 size 属性为 80,表示 loading 动画的大小为 80px;Typography 组件用于展示 "Loading..." 的文本。 3. 如果需要在 loading 时隐藏其他内容,可以使用 MUI 的 Box 组件的 visibility 属性来实现: ```javascript <Box sx={{ visibility: isLoading ? 'hidden' : 'visible' }}> {/* 这里放需要展示的内容 */} </Box> ``` 其中,isLoading 表示 loading 的状态,如果为 true,则隐藏 Box 中的内容;如果为 false,则展示 Box 中的内容。 完整示例代码如下: ```javascript import CircularProgress from '@mui/material/CircularProgress'; import Box from '@mui/material/Box'; import Typography from '@mui/material/Typography'; function MyComponent() { const [isLoading, setLoading] = useState(false); // 模拟 loading 状态的变化 useEffect(() => { setLoading(true); setTimeout(() => { setLoading(false); }, 3000); }, []); return ( <Box sx={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}> {isLoading && ( <Box sx={{ position: 'absolute', top: 0, left: 0, width: '100%', height: '100%', backgroundColor: 'rgba(255, 255, 255, 0.5)', zIndex: 1 }}> <Box sx={{ display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', position: 'absolute', top: '50%', left: '50%', transform: 'translate(-50%, -50%)' }}> <CircularProgress size={80} /> <Typography variant="h6" sx={{ mt: 2 }}> Loading... </Typography> </Box> </Box> )} <Box sx={{ visibility: isLoading ? 'hidden' : 'visible' }}> {/* 这里放需要展示的内容 */} </Box> </Box> ); } ``` 上述代码中还包含了一个定位在最上层的半透明层,用于遮盖其他内容,以达到更好的 loading 效果

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值