晚上吃什么呢?

2018年7月29日21:02:17 6 阅读(2,626)
本文最后更新于2018年8月1日,已超过一年没有更新,如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!

你是否不知道晚上吃啥?你是否想吃某个范围的事物但是不知道选哪个?本文帮你解决这个问题 [酷]

主要原理就是计时器+random一个数组,效果如图所示:

晚上吃什么呢?

HTML部分

  1. <h1 style="color:#40AA53; text-align:center;">吃啥呢!</h1>
  2. <div id="wrapper">
  3.     <div id="Result" style="color:#40AA53">
  4.         <span id="what">0</span>
  5.     </div>
  6. </div>
  7. <div id="Button">
  8.     <input type="button" id="start" value="不行,换一个">
  9. </div><br/>
  10. <p><span id="cfg" title="编辑候选菜单">{ 自定义候选菜单 }</span></p>
  11.      <div id="popbox-wrapper">
  12.          <div id="popbox">
  13.              <h3>食物菜单</h3>
  14.              <textarea rows="7" cols="51" id="list" placeholder="在这里输入候选食物,食物之间以空格分开..."></textarea><input type="button" value="确定" id="ok" class="btn btn-primary"/>
  15.          </div>
  16.      </div>
  17. <script src="script.js"></script>

JS部分

  1. $(function () {
  2.     var running = 0,
  3.         h1 = $("h1"),
  4.         h1b4 = $("span", h1).eq(0),
  5.         h1a5 = $("span", h1).eq(-1),
  6.         what = $("#what"),
  7.         btn = $("#start"),
  8.         popup = $("#popbox-wrapper"),
  9.         list = $("#list"),
  10.         book = [
  11.             "输入候选食物"]
  12.         times = 0,
  13.         hour = (new Date).getHours();
  14.     btn.val("开始").removeAttr("disabled").click(function () {
  15.         var l = list.val().replace(/ +/g, " ").replace(/^ | $/g, "").split(" ");
  16.         if (l.length == 1 && l[0] != ""return alert("→_→ 耍我是吧,一个有什么好选的!");
  17.         if (l.length == 1) return alert("啥也没有,吃西北风去啊?");
  18.         if (!running) {
  19.             times++;
  20.             if (times == 3) {
  21.                 $("<span class='tip'>都不想吃?可以编辑菜单的哦!</span>").css({
  22.                     opacity: 0
  23.                 }).appendTo("#wrapper").animate({
  24.                     left: "-=10px",
  25.                     opacity: 1
  26.                 });
  27.                 btn.val("继续");
  28.                 $("#cfg").add(btn).one("click"function () {
  29.                     $(".tip").animate({
  30.                         left: "+=10px",
  31.                         opacity: 0
  32.                     }, function () {
  33.                         $(this).remove();
  34.                     });
  35.                 });
  36.                 return false;
  37.             };
  38.             $(this).val("停止");
  39.             timer = setInterval(function () {
  40.                 var r = MOFUN.random(l.length),
  41.                     food = l[r - 1],
  42.                     rTop = MOFUN.random($(document).height() - 100),
  43.                     rLeft = MOFUN.random($(document).width() - 50),
  44.                     rSize = MOFUN.random(37, 14);
  45.                 what.html(food);
  46.                 $("<span class='temp'>" + food + "</span>").css({
  47.                     "display""none",
  48.                     "top": rTop,
  49.                     "left": rLeft,
  50.                     "color""rgba(0,0,0,." + Math.random() + ")",
  51.                     "fontSize": rSize + "px"
  52.                 }).appendTo("body").fadeIn("slow"function () {
  53.                     $(this).fadeOut("slow"function () {
  54.                         $(this).remove();
  55.                     });
  56.                 });
  57.             }, 60);
  58.             running = 1;
  59.             $("#shareto").fadeOut(function () {
  60.                 $(this).remove()
  61.             });
  62.         } else {
  63.             btn.val("不行,换一个");
  64.             clearInterval(timer);
  65.             running = 0;
  66.         };
  67.     });
  68.     h1.click(function () {
  69.         if (!running) {
  70.             h1.css("position""relative").stop().animate({
  71.                 left: "-20px"
  72.             }, 50).animate({
  73.                 left: "20px"
  74.             }, 50).animate({
  75.                 left: "-10px"
  76.             }, 50).animate({
  77.                 left: "10px"
  78.             }, 50).animate({
  79.                 left: "-5px"
  80.             }, 50).animate({
  81.                 left: "5px"
  82.             }, 50).animate({
  83.                 left: 0
  84.             }, 50, function () {
  85.                 $(this).removeAttr("style");
  86.             });
  87.         };
  88.     });
  89.     $("#cfg").click(function () {
  90.         running ? alert("还在选呢!") : popup.fadeIn(function () {
  91.             var t = list.val();
  92.             list.focus().val("").val(t)
  93.         });
  94.     });
  95.     $("#ok").click(function () {
  96.         popup.fadeOut();
  97.     });
  98.     if (hour < 9 || hour >= 23) {
  99.         change(0);
  100.     } else if (hour < 13) {
  101.         change(1);
  102.     };
  103.     function change(i) {
  104.         i = i || 0;
  105.         $("<i class='notdinner'></i>").css({
  106.             top: "-44px",
  107.             opacity: 0
  108.         }).appendTo("#wrapper").animate({
  109.             top: "+=10px",
  110.             opacity: 1
  111.         }, 1e3).delay(6e3).fadeOut("slow"function () {
  112.             $(this).remove();
  113.         });
  114.         list.val(book[i][1]);
  115.         var t = i + 1;
  116.         while (t > 0) {
  117.             book.push(book.shift());
  118.             t--;
  119.         };
  120.     };
  121.     document.onkeydown = function (e) {
  122.         e = e || window.event;
  123.         if (e.keyCode == 32 && !popup.is(":visible")) $("#start").trigger("click");
  124.     };
  125. });

CSS部分

  1. body{
  2.     background-color:#fff;
  3.     text-align:center;
  4.     padding-top:50px;
  5. }
  6. #wrapper {
  7.     positionrelative;
  8.     margin50px auto;
  9.     width365px;
  10.     padding33px 25px 29px;
  11.     backgroundwhite;
  12.     border-bottom1px solid #c4c4c4;
  13.     border-radius: 5px;
  14.     -webkit-box-shadow: 0 1px 5px rgba(0, 0, 0, 0.25);
  15.     box-shadow: 0 1px 5px rgba(0, 0, 0, 0.25);
  16. }
  17. #Result{
  18.     margin:0 auto;
  19.     text-align:center;
  20.     padding:50px 0;
  21. }
  22. #Result:before {
  23.     content'';
  24.     positionabsolute;
  25.     top: 0;
  26.     left: 0;
  27.     rightright: 0;
  28.     height8px;
  29.     background#c4e17f;
  30.     border-radius: 5px 5px 0 0;
  31.     background-image: -webkit-linear-gradient(left#c4e17f#c4e17f 12.5%, #f7fdca 12.5%, #f7fdca 25%, #fecf71 25%, #fecf71 37.5%, #f0776c 37.5%, #f0776c 50%, #db9dbe 50%, #db9dbe 62.5%, #c49cde 62.5%, #c49cde 75%, #669ae1 75%, #669ae1 87.5%, #62c2e4 87.5%, #62c2e4);
  32.     background-image: -moz-linear-gradient(left#c4e17f#c4e17f 12.5%, #f7fdca 12.5%, #f7fdca 25%, #fecf71 25%, #fecf71 37.5%, #f0776c 37.5%, #f0776c 50%, #db9dbe 50%, #db9dbe 62.5%, #c49cde 62.5%, #c49cde 75%, #669ae1 75%, #669ae1 87.5%, #62c2e4 87.5%, #62c2e4);
  33.     background-image: -o-linear-gradient(left#c4e17f#c4e17f 12.5%, #f7fdca 12.5%, #f7fdca 25%, #fecf71 25%, #fecf71 37.5%, #f0776c 37.5%, #f0776c 50%, #db9dbe 50%, #db9dbe 62.5%, #c49cde 62.5%, #c49cde 75%, #669ae1 75%, #669ae1 87.5%, #62c2e4 87.5%, #62c2e4);
  34.     background-image: linear-gradient(to rightright#c4e17f#c4e17f 12.5%, #f7fdca 12.5%, #f7fdca 25%, #fecf71 25%, #fecf71 37.5%, #f0776c 37.5%, #f0776c 50%, #db9dbe 50%, #db9dbe 62.5%, #c49cde 62.5%, #c49cde 75%, #669ae1 75%, #669ae1 87.5%, #62c2e4 87.5%, #62c2e4);
  35. }
  36. #what{
  37.     font-size:50pt;
  38.     font-family:Verdana
  39. }
  40. #Button{
  41.     margin:50px 0 0 0;
  42. }
  43. #Button input{
  44.     font-size:30px;
  45.     padding:0 ;
  46.     background-color#2ecc71;
  47.     box-shadow: 0px 5px 0px 0px #15B358;
  48.     outline:none;
  49. }
  50. #Button input:hover {
  51.     background-color#48E68B;
  52.     outline:none;
  53. }
  54. #Button input:active {
  55.     transform: translate(0px5px);
  56.     -webkit-transform: translate(0px5px);
  57.     box-shadow: 0px 1px 0px 0px;
  58.     outline:none;
  59. }
  60. #start{
  61.     background-color:#40AA53;
  62.     border:1px solid #40AA53;
  63.     width:200px;
  64.     height:65px;
  65.     margin:0em auto;
  66.     font-size:1em;
  67.     border-radius:10px;
  68.     color:#FFF;
  69.     -webkit-appearance:none;
  70.     -moz-appearance:none
  71. }
  72. #footer {
  73.     margin-top100px;
  74.     border-top1px solid #ccc;
  75.     padding20px 0;
  76.     color#ccc;
  77. }
  78. #popbox-wrapper{
  79.     positionabsolute;
  80.     top: 0;
  81.     left: 0;
  82.     displaynone;
  83.     width: 100%;
  84.     height: 100%;
  85. }
  86. #popbox{
  87.     positionabsolute;
  88.     top: 50%;
  89.     left: 50%;
  90.     margin-top: -140px;
  91.     margin-left: -200px;
  92.     width400px;
  93.     height300px;
  94.     -webkit-border-radius: 5px;
  95.     -moz-border-radius: 5px;
  96.     border-radius: 5px;
  97.     background#fff;
  98.     -webkit-box-shadow: 0 0 0 5px rgba(0,0,0,.2);
  99.     -moz-box-shadow: 0 0 0 5px rgba(0,0,0,.2);
  100.     box-shadow: 0 0 0 5px rgba(0,0,0,.2);
  101. +border5px solid #5D5D5C;
  102. }
  103. .ie8 #popbox { border5px solid #5D5D5C }
  104. #popbox h3{
  105.     margin: 0;
  106.     padding20px 0;
  107.     border-bottom1px solid #bbb;
  108.     -webkit-border-radius: 5px 5px 0 0;
  109.     -moz-border-radius: 5px 5px 0 0;
  110.     border-radius: 5px 5px 0 0;
  111.     background#E7E7E7 url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAA7CAIAAAA1lncVAAAAIklEQVQI12P4//8/0////5kYGBjgNC42PnWkilHDDBLtAgC6y0sq4aOFWQAAAABJRU5ErkJggg=="repeat-x 0 100%;
  112.     color#333;
  113.     text-aligncenter;
  114.     font-weight: 700;
  115.     font-size19px;
  116.     line-height: 1;
  117. }
  118. #popbox textarea{
  119.     overflowauto;
  120.     margin10px;
  121.     width380px;
  122.     height160px;
  123.     border: 0;
  124.     background#efefef;
  125.     font-size14px;
  126.     resize: none;
  127.     padding10px;
  128. }
  129. #cfg {
  130.     color#666;
  131.     cursorpointer;
  132. }
  133. #cfg:hover {
  134.     font-weight: 700;
  135. }
  136. .temp{
  137.     positionabsolute;
  138.     z-index: -1;
  139.     color#777;
  140. }

Demo:http://garden.haoht123.com/food

感谢

本文基于小蝴蝶的基础上添加了可以自定义候选菜单的功能以及一些效果。

小蝴蝶  :今晚吃啥呢?——随机摇饭店前后端代码 

 

完。


 

weinxin
微信小程序
互联网开发,终身学习者,欢迎您的关注!
舍得

发表评论

不高兴 彩虹 吃瓜 丢翔 乖 滑稽 花心 惊哭 惊讶 挤眼 酷 伤心 帅吗? 礼物 玫瑰 怒 生气 喷 睡觉 太开心 小九九 啊
太阳 吐舌 委屈 笑眼 星星月亮 心碎 咦 阴险 疑问 真棒 偷笑 斜眼笑 震惊 略 哈欠 无奈哭 抠鼻 哼 期待 懒得理你 爱心 蜡烛

目前评论:6   其中:访客  3   博主  3

    • 云柯博客 云柯博客 2

      …… 我都想吃呢[帅吗?]

      • 懿古今 懿古今 3

        这才是生活,想吃啥就吃啥,而我是生存,想吃什么都要先看看口袋,然后还是喝白粥吧

          • 舍得 舍得

            @懿古今 你是以后的我。我现在是一人吃饱,全家不饿 [无奈哭]

          • Roogle Roogle 0

            选择困难症人的终极福利