【demo】原生js实现纯进度条

  1. setTimeout
<html> 
<head> 
<title>进度条</title> 
  <style type="text/css">  
  .container{  
     width: 450px;  
     border: 1px solid #000;  
     height: 25px;  
   }
  #bar{  
     background: #000;  
     float: left; 
     height: 100%;  
     text-align: center;  
   }  
  </style>    
</head> 
<body> 
  <div class="container"> 
    <div id="bar" style="width:0%;"></div>  
  </div>  
  <span id="total"></span> 
</body> 
<script type="text/javascript">  
  function run(){  
    var bar = document.getElementById("bar"); 
    var total = document.getElementById("total"); 
    bar.style.width = parseInt(bar.style.width) + 1 + "%";  
    total.innerHTML = bar.style.width; 
    if(bar.style.width == "100%"){  
      clearTimeout(timeout); 
      return;  //没有这行就一直加载超出100%
    } 
    var timeout = setTimeout("run()",100); 
  } 

  window.onload = function(){  
     run(); 
  }  
</script> 
</html>

在这里插入图片描述

  1. setInterval
<html>  
<head>  
<title>进度条</title>  
<style type="text/css">  
.processcontainer{  
   width: 450px;  
   border: 1px solid #000;  
   height: 25px;  
   color: #fff;  
 } 
#processbar{  
   background: #000;  
   float: left; 
   height: 100%;  
   text-align: center;
 }  
</style>  

</head>  
<body>  
  <div class="processcontainer">  
   <div id="processbar" style="width:0%;"></div>  
  </div>  
</body>  
<script type="text/javascript">  
 function setProcess(){  
  var processbar = document.getElementById("processbar");  
  processbar.style.width = parseInt(processbar.style.width) + 1 + "%";
  processbar.innerHTML = processbar.style.width;  
  if(processbar.style.width == "100%"){  
    clearInterval(bartimer);  
  }  
 }  
var bartimer = setInterval(function(){setProcess();},100);  
window.onload = function(){  
   bartimer;  
}  
</script>  
</html>

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以提供一个简单的限时秒杀demo原生JavaScript实现。以下是基本的HTML和CSS代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>限时秒杀</title> <style> .seckill-item { border: 1px solid #ccc; margin: 10px; padding: 10px; width: 200px; height: 200px; float: left; } .seckill-item img { width: 100%; height: 120px; } .seckill-item h3 { font-size: 16px; margin: 10px 0; } .seckill-item .price { font-size: 20px; color: red; margin: 10px 0; } .seckill-item .stock { margin: 10px 0; } .seckill-item .btn-seckill { background-color: #f00; color: #fff; border: none; padding: 5px 10px; margin-top: 10px; cursor: pointer; } </style> </head> <body> <div id="seckill-list"> <div class="seckill-item"> <img src="img/1.jpg" alt="商品1"> <h3>商品1</h3> <div class="price">¥99.00</div> <div class="stock">库存: 100</div> <button class="btn-seckill" data-id="1" data-price="99.00" data-stock="100" data-start="2022/01/01 00:00:00" data-end="2022/01/01 23:59:59">立即秒杀</button> </div> <div class="seckill-item"> <img src="img/2.jpg" alt="商品2"> <h3>商品2</h3> <div class="price">¥199.00</div> <div class="stock">库存: 50</div> <button class="btn-seckill" data-id="2" data-price="199.00" data-stock="50" data-start="2022/01/01 00:00:00" data-end="2022/01/01 23:59:59">立即秒杀</button> </div> </div> <script src="seckill.js"></script> </body> </html> ``` 在这个页面中,我们设置了两个商品的信息,包括商品名称、价格、库存量和秒杀活动的开始时间和结束时间。每个商品都有一个“立即秒杀”按钮,它包含了商品的相关信息,通过data-*属性来存储。 以下是JavaScript代码的实现: ```javascript // 获取秒杀按钮列表 var seckillBtns = document.querySelectorAll('.btn-seckill'); // 循环遍历秒杀按钮列表,为每个按钮添加点击事件 seckillBtns.forEach(function(btn) { btn.addEventListener('click', function() { // 获取秒杀商品的相关信息 var id = this.getAttribute('data-id'); var price = this.getAttribute('data-price'); var stock = this.getAttribute('data-stock'); var start = this.getAttribute('data-start'); var end = this.getAttribute('data-end'); // 判断当前时间是否在秒杀活动时间范围内 var now = new Date(); var startTime = new Date(start); var endTime = new Date(end); if (now < startTime || now > endTime) { alert('秒杀活动还未开始或已经结束!'); return false; } // 判断商品库存是否充足 if (stock <= 0) { alert('商品库存不足!'); return false; } // 发送秒杀请求 alert('恭喜,秒杀成功!'); // 更新商品库存 stock--; this.setAttribute('data-stock', stock); this.parentNode.querySelector('.stock').innerHTML = '库存: ' + stock; }); }); ``` 这段代码的主要功能是为每个“立即秒杀”按钮添加点击事件,当用户点击按钮时,首先获取商品的相关信息,然后判断当前时间是否在秒杀活动时间范围内,以及商品库存是否充足。如果满足条件,就发送秒杀请求,更新商品库存信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值