溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

jQuery基于隨機(jī)數(shù)解決中午吃什么去哪吃問題示例

發(fā)布時(shí)間:2020-10-19 16:29:10 來源:腳本之家 閱讀:110 作者:csmzl 欄目:web開發(fā)

本文實(shí)例講述了jQuery基于隨機(jī)數(shù)解決中午吃什么去哪吃問題。分享給大家供大家參考,具體如下:

一個(gè)解決中午吃什么去哪吃的程序

這下不用每天都糾結(jié)吃什么了!

代碼功能類似于前面一篇《jQuery實(shí)現(xiàn)的老虎機(jī)跑動(dòng)效果》,很有意思

例一:

<html>
  <head>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <style type="text/css">
      #bigDiv div{
        height:50px;
        width:50px;
        float:left;
        background-color:red;
        margin-left:5px;
        visibility: hidden;
      }
      #bigDiv p{
        margin-left: 10px;
      }
    </style>
  </head>
  <body>
  <div id = bigDiv>
    <div><p>吃面</p></div>
    <div><p>吃飯</p></div>
    <div><p>蘭州</p></div>
    <div><p>隨便</p></div>
    <div><p>炒飯</p></div>
    <div><p>一期</p></div>
    <div><p>二期</p></div>
    <div><p>全家</p></div>
    <div><p>西北</p></div>
    <div><p>謝謝</p></div>
  </div>
    <br/><br/><br/><br/>
    <input type="button" id="startBtn" value="開 始" onclick="startRun()">
    <input type="button" id="confirmBtn" value="確 定" onclick="stopRun()">
    <script language="javascript">
      var allDiv = $("#bigDiv").find("div");
      var t;
      function startRun(){
        var index = 11;
        $(allDiv).each(function(i){
          if($(this).css("visibility")!="hidden"){
            index = i;
          }
        });
        if(index == 11){
          index = parseInt(9*Math.random());
        }
        $(allDiv).eq(index).css("visibility","visible");
        setTimeout(function(){stepRun(index);},50);
      }
      function stepRun(index){
        if($(allDiv).eq(index).css("visibility")!="hidden")
        {
          $(allDiv).eq(index).css("visibility","hidden");
          if(index==9){
            $(allDiv).eq(0).css("visibility","visible");
            t = setTimeout(function(){stepRun(0)},50);
          }else{
            $(allDiv).eq(index+1).css("visibility","visible");
            t = setTimeout(function(){stepRun(++index)},50);
          }
        }
      }
      function stopRun()
      {
        clearTimeout(t);
      }
    </script>
  </body>
</html>

使用在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測(cè)試上述代碼,可得如下運(yùn)行效果:

jQuery基于隨機(jī)數(shù)解決中午吃什么去哪吃問題示例

例二:?jiǎn)伟粹o實(shí)現(xiàn)

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <style type="text/css">
      #bigDiv div{
        height:50px;
        width:50px;
        float:left;
        background-color:red;
        margin-left:5px;
        visibility: hidden;
      }
    </style>
  </head>
  <body>
    <div align="center">
      <div  id = bigDiv>
        <div><p>火鍋</p></div>
        <div><p>依鮮</p></div>
        <div><p>蘭州</p></div>
        <div><p>隨便</p></div>
        <div><p>炒飯</p></div>
        <div><p>一期</p></div>
        <div><p>二期</p></div>
        <div><p>全家</p></div>
        <div><p>西北</p></div>
        <div><p>謝謝</p></div>
      </div>
      <br/><br/><br/><br/>
      <input type="button" id="startBtn" value="開 始" onclick="startRun()">
    </div>
    <script language="javascript">
      var allDiv = $("#bigDiv").find("div");
      var t;
      var stop = true;
      function startRun(){
        if(stop){
          $("#startBtn").val("停 止");
          var index = 11;//11取值范圍是大于已有選項(xiàng)項(xiàng)數(shù)
          $(allDiv).each(function(i){
            if($(this).css("visibility")!="hidden"){
              index = i;
            }
          });
          if(index == 11){
            index = parseInt(9*Math.random());
          }
          $(allDiv).eq(index).css("visibility","visible");
          setTimeout(function(){stepRun(index);},50);
          stop = false;
        }else{
          $("#startBtn").val("開 始");
          clearTimeout(t);
          stop = true;
        }
      }
      function stepRun(index){
        if($(allDiv).eq(index).css("visibility")!="hidden")
        {
          $(allDiv).eq(index).css("visibility","hidden");
          if(index==9){
            $(allDiv).eq(0).css("visibility","visible");
            t = setTimeout(function(){stepRun(0)},50);
          }else{
            $(allDiv).eq(index+1).css("visibility","visible");
            t = setTimeout(function(){stepRun(++index)},50);
          }
        }
      }
    </script>
  </body>
</html>

使用在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測(cè)試上述代碼,可得如下運(yùn)行效果:

jQuery基于隨機(jī)數(shù)解決中午吃什么去哪吃問題示例

更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery操作DOM節(jié)點(diǎn)方法總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jquery中Ajax用法總結(jié)》、《jQuery常見經(jīng)典特效匯總》、《jQuery動(dòng)畫與特效用法總結(jié)》及《jquery選擇器用法總結(jié)》

希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI