提交表单时的等待效果动画

在某些表单提交中,为了更好的用户体验,会加一个等待框,以便于告诉用户系统正在进行数据提交。下面是自己用js和css写的一个等待效果。如图。

等待效果

 

js代码:

function showWaiting(){
	var node=document.createElement("div");  //创建一个div元素节点,作为整个背景的容器
	var nodeClass=document.createAttribute("class"); //创建class元素属性
	var nodeStyle = document.createAttribute("style"); //创建style元素属性
	var nodeName = document.createAttribute("name"); //创建name元素属性
	nodeName.value = "divWaiting"; //给元素节点命名
	nodeClass.value = "div-waiting"; //元素属性赋值
	nodeStyle.value = "height:"+window.innerHeight + "px; width:"+window.innerWidth+"px;";
	node.setAttributeNode(nodeClass); //设置元素节点的属性及值
	node.setAttributeNode(nodeStyle);
	node.setAttributeNode(nodeName);
	var iconNode = document.createElement("div");  //创建一个div元素节点,作为旋转图标的容器
	var iconClass = document.createAttribute("class");
	iconClass.value = "icon-waiting";
        //iconClass.value = "fa fa-spinner fa-pulse icon-position";
	iconNode.setAttributeNode(iconClass);
	
	node.appendChild(iconNode);  //将图标节点放到整个背景的元素节点
	document.body.appendChild(node); //将整个背景div插入到body中
}

function closeWaiting(){
	var wait = document.getElementsByName("divWaiting"); //获取name为divWaiting的元素节点
	console.log(wait);
    //遍历所有的节点,将body中的所有等待旋转效果去掉
	for(var i=wait.length - 1; i>=0; i--){  
		document.body.removeChild(wait[i]); 
	}
}

 

css代码:

@-webkit-keyframes spin {
	  	to {
	    	-webkit-transform: rotate(360deg);
	        transform: rotate(360deg);
	  	}
	}
	
	@keyframes spin {
		to {
			-webkit-transform: rotate(360deg);
			transform: rotate(360deg);
		}
	}
	.div-waiting{
		position: fixed;
	    z-index: 998;
	    top: 0;
	    right: 0;
	    bottom: 0;
	    left: 0;
		opacity: 1;
	    background: rgba(0,0,0,0.2);
	    vertical-align: middle;
	    text-align: center;
	}
	.icon-waiting{
		position: relative;
		top: 48%;
	    width: 5rem;
	    height: 5rem;
	    margin: 0 auto;
	    border-radius: 50%;
	    border: 0.5rem solid rgba(21, 21, 21, 0.4);
	    border-top-color: #e1e1e1;
	    -webkit-animation: 1.5s spin infinite linear;
	    animation: 1.5s spin infinite linear;
	}
        .icon-position{
            position: relative;
            top: 48%;
            margin: 0 auto;
            font-size: 30px;
        }

还可以引用fontawesome的样式 fa-spin使任意图标旋转:

使用 fa-spin 类来使任意图标旋转,现在您还可以使用 fa-pulse 来使其进行8方位旋转。尤其适合 fa-spinnerfa-refresh 和 fa-cog 。

将iconClass节点的value赋值为 “fa fa-spinner fa-pulse icon-position”

 

以下是全部代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>等待旋转效果</title>
    <link rel="stylesheet" href="../extends/font-awesome-4.7.0/css/font-awesome.css">
    <style type="text/css">
        @-webkit-keyframes spin {
            to {
                -webkit-transform: rotate(360deg);
                transform: rotate(360deg);
            }
        }

        @keyframes spin {
            to {
                -webkit-transform: rotate(360deg);
                transform: rotate(360deg);
            }
        }
        .div-waiting{
            position: fixed;
            z-index: 998;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
            opacity: 1;
            background: rgba(0,0,0,0.2);
            vertical-align: middle;
            text-align: center;
        }
        .icon-waiting{
            position: relative;
            top: 48%;
            width: 5rem;
            height: 5rem;
            margin: 0 auto;
            border-radius: 50%;
            border: 0.5rem solid rgba(21, 21, 21, 0.4);
            border-top-color: #e1e1e1;
            -webkit-animation: 1.5s spin infinite linear;
            animation: 1.5s spin infinite linear;
        }
        .icon-position{
            position: relative;
            top: 48%;
            margin: 0 auto;
            font-size: 30px;
        }
        button{
            padding: 6px 12px;
            margin: 10px;
            background: #00bbee;
            border: 1px solid #00bfff;
            border-radius: 3px;
        }
    </style>
</head>
<body>
    <button onclick="showWaiting()">显示</button>
    <button onclick="closeWaiting()" style="z-index: 10000;position: relative;">关闭</button>
</body>

<script type="text/javascript">
    function showWaiting(){
        var node=document.createElement("div");  //创建一个div元素节点,作为整个背景的容器
        var nodeClass=document.createAttribute("class"); //创建class元素属性
        var nodeStyle = document.createAttribute("style"); //创建style元素属性
        var nodeName = document.createAttribute("name"); //创建name元素属性
        nodeName.value = "divWaiting"; //给元素节点命名
        nodeClass.value = "div-waiting"; //元素属性赋值
        nodeStyle.value = "height:"+window.innerHeight + "px; width:"+window.innerWidth+"px;";
        node.setAttributeNode(nodeClass); //设置元素节点的属性及值
        node.setAttributeNode(nodeStyle);
        node.setAttributeNode(nodeName);
        var iconNode = document.createElement("div");  //创建一个div元素节点,作为旋转图标的容器
        var iconClass = document.createAttribute("class");
        iconClass.value = "icon-waiting";
        //iconClass.value = "fa fa-spinner fa-pulse icon-position";
        iconNode.setAttributeNode(iconClass);

        node.appendChild(iconNode);  //将图标节点放到整个背景的元素节点
        document.body.appendChild(node); //将整个背景div插入到body中
    }

    function closeWaiting(){
        var wait = document.getElementsByName("divWaiting"); //获取name为divWaiting的元素节点
        console.log(wait);
        //遍历所有的节点,将body中的所有等待旋转效果去掉
        for(var i=wait.length - 1; i>=0; i--){
            document.body.removeChild(wait[i]);
        }
    }
</script>
</html>

 

以下是js代码,可写为js文件,以后使用调用js文件即可

function showWaiting(){
    var node=document.createElement("div");  //创建一个div元素节点,作为整个背景的容器
    var nodeClass=document.createAttribute("class"); //创建class元素属性
    var nodeStyle = document.createAttribute("style"); //创建style元素属性
    var nodeName = document.createAttribute("name"); //创建name元素属性
    nodeName.value = "divWaiting"; //给元素节点命名
    var nodeStyleVal = "position: fixed; z-index: 998; top: 0; right: 0; bottom: 0; left: 0; opacity: 1; background: rgba(0,0,0,0.2); vertical-align: middle; text-align: center;"
    	nodeStyleVal += "height:"+window.innerHeight + "px; width:"+window.innerWidth+"px;";
    nodeStyle.value = nodeStyleVal;
    node.setAttributeNode(nodeClass); //设置元素节点的属性及值
    node.setAttributeNode(nodeStyle);
    node.setAttributeNode(nodeName);
    var iconNode = document.createElement("div");  //创建一个div元素节点,作为旋转图标的容器
    var iconClass = document.createAttribute("class");
    iconClass.value = "fa fa-spinner fa-pulse"; //必须引用font-awesome
    var iconStyle = document.createAttribute("style");
    var iconStyleValue = "position: relative;top: 48%;margin: 0 auto;font-size: 30px;";
    iconStyle.value = iconStyleValue;
    iconNode.setAttributeNode(iconStyle);
    iconNode.setAttributeNode(iconClass);

    node.appendChild(iconNode);  //将图标节点放到整个背景的元素节点
    document.body.appendChild(node); //将整个背景div插入到body中
}

function closeWaiting(){
    var wait = document.getElementsByName("divWaiting"); //获取name为divWaiting的元素节点
    //遍历所有的节点,将body中的所有等待旋转效果去掉
    for(var i=wait.length - 1; i>=0; i--){
        document.body.removeChild(wait[i]);
    }
}

 

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
jQuery 是一种流行的 JavaScript 库,用于简化网页开发,尤其是处理 DOM 操作和创建动画效果。对于登录/注册单的动画效果,jQuery 提供了丰富的 API 来增强用户体验。以下是一些常见的 jQuery 动画应用到登录/注册单的例子: 1. **淡入淡出(Fade Effect)**:当单元素显示或隐藏时,可以使用 `fadeIn()` 和 `fadeOut()` 方法创建淡入淡出的效果。 ```javascript $("#login-form").fadeIn(500); $("#login-form").fadeOut(500, function() { // 登录/注册操作完成后的回调 }); ``` 2. **滑动(Slide Effect)**:单元素可以水平或垂直滑动进入或离开视图。 ```javascript $("#login-form").slideUp(500); // 上滑 $("#login-form").slideDown(500); // 下滑 ``` 3. **弹跳(Bounce Effect)**:为单添加一种轻快的弹跳效果,使用 `slideUp()` 或 `slideDown()` 的参数设置为 `'slow'` 结合 `bounce` 效果。 ```javascript $("#login-form").slideUp('slow', 'bounce'); ``` 4. **逐项出现(SlideToggle)**:可以使用 `slideToggle()` 方法一次性让多个输入字段显示或隐藏。 ```javascript $(".input-field").slideToggle(500); ``` 5. **过渡动画(Transition Effect)**:利用 CSS3 过渡配合 jQuery,为按钮添加平滑的过渡效果,如鼠标悬停时改变颜色。 ```javascript $(".login-button").hover(function() { $(this).css("background-color", "blue").transition({background-color: "red"}, 1000); }, function() { $(this).css("background-color", "initial"); }); ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值