你是否不知道晚上吃啥?你是否想吃某个范围的事物但是不知道选哪个?本文帮你解决这个问题
主要原理就是计时器+random一个数组,效果如图所示:
HTML部分
- <h1 style="color:#40AA53; text-align:center;">吃啥呢!</h1>
- <div id="wrapper">
- <div id="Result" style="color:#40AA53">
- <span id="what">0</span>
- </div>
- </div>
- <div id="Button">
- <input type="button" id="start" value="不行,换一个">
- </div><br/>
- <p><span id="cfg" title="编辑候选菜单">{ 自定义候选菜单 }</span></p>
- <div id="popbox-wrapper">
- <div id="popbox">
- <h3>食物菜单</h3>
- <textarea rows="7" cols="51" id="list" placeholder="在这里输入候选食物,食物之间以空格分开..."></textarea><input type="button" value="确定" id="ok" class="btn btn-primary"/>
- </div>
- </div>
- <script src="script.js"></script>
JS部分
- $(function () {
- var running = 0,
- h1 = $("h1"),
- h1b4 = $("span", h1).eq(0),
- h1a5 = $("span", h1).eq(-1),
- what = $("#what"),
- btn = $("#start"),
- popup = $("#popbox-wrapper"),
- list = $("#list"),
- book = [
- "输入候选食物"]
- times = 0,
- hour = (new Date).getHours();
- btn.val("开始").removeAttr("disabled").click(function () {
- var l = list.val().replace(/ +/g, " ").replace(/^ | $/g, "").split(" ");
- if (l.length == 1 && l[0] != "") return alert("→_→ 耍我是吧,一个有什么好选的!");
- if (l.length == 1) return alert("啥也没有,吃西北风去啊?");
- if (!running) {
- times++;
- if (times == 3) {
- $("<span class='tip'>都不想吃?可以编辑菜单的哦!</span>").css({
- opacity: 0
- }).appendTo("#wrapper").animate({
- left: "-=10px",
- opacity: 1
- });
- btn.val("继续");
- $("#cfg").add(btn).one("click", function () {
- $(".tip").animate({
- left: "+=10px",
- opacity: 0
- }, function () {
- $(this).remove();
- });
- });
- return false;
- };
- $(this).val("停止");
- timer = setInterval(function () {
- var r = MOFUN.random(l.length),
- food = l[r - 1],
- rTop = MOFUN.random($(document).height() - 100),
- rLeft = MOFUN.random($(document).width() - 50),
- rSize = MOFUN.random(37, 14);
- what.html(food);
- $("<span class='temp'>" + food + "</span>").css({
- "display": "none",
- "top": rTop,
- "left": rLeft,
- "color": "rgba(0,0,0,." + Math.random() + ")",
- "fontSize": rSize + "px"
- }).appendTo("body").fadeIn("slow", function () {
- $(this).fadeOut("slow", function () {
- $(this).remove();
- });
- });
- }, 60);
- running = 1;
- $("#shareto").fadeOut(function () {
- $(this).remove()
- });
- } else {
- btn.val("不行,换一个");
- clearInterval(timer);
- running = 0;
- };
- });
- h1.click(function () {
- if (!running) {
- h1.css("position", "relative").stop().animate({
- left: "-20px"
- }, 50).animate({
- left: "20px"
- }, 50).animate({
- left: "-10px"
- }, 50).animate({
- left: "10px"
- }, 50).animate({
- left: "-5px"
- }, 50).animate({
- left: "5px"
- }, 50).animate({
- left: 0
- }, 50, function () {
- $(this).removeAttr("style");
- });
- };
- });
- $("#cfg").click(function () {
- running ? alert("还在选呢!") : popup.fadeIn(function () {
- var t = list.val();
- list.focus().val("").val(t)
- });
- });
- $("#ok").click(function () {
- popup.fadeOut();
- });
- if (hour < 9 || hour >= 23) {
- change(0);
- } else if (hour < 13) {
- change(1);
- };
- function change(i) {
- i = i || 0;
- $("<i class='notdinner'></i>").css({
- top: "-44px",
- opacity: 0
- }).appendTo("#wrapper").animate({
- top: "+=10px",
- opacity: 1
- }, 1e3).delay(6e3).fadeOut("slow", function () {
- $(this).remove();
- });
- list.val(book[i][1]);
- var t = i + 1;
- while (t > 0) {
- book.push(book.shift());
- t--;
- };
- };
- document.onkeydown = function (e) {
- e = e || window.event;
- if (e.keyCode == 32 && !popup.is(":visible")) $("#start").trigger("click");
- };
- });
CSS部分
- body{
- background-color:#fff;
- text-align:center;
- padding-top:50px;
- }
- #wrapper {
- position: relative;
- margin: 50px auto;
- width: 365px;
- padding: 33px 25px 29px;
- background: white;
- border-bottom: 1px solid #c4c4c4;
- border-radius: 5px;
- -webkit-box-shadow: 0 1px 5px rgba(0, 0, 0, 0.25);
- box-shadow: 0 1px 5px rgba(0, 0, 0, 0.25);
- }
- #Result{
- margin:0 auto;
- text-align:center;
- padding:50px 0;
- }
- #Result:before {
- content: '';
- position: absolute;
- top: 0;
- left: 0;
- rightright: 0;
- height: 8px;
- background: #c4e17f;
- border-radius: 5px 5px 0 0;
- 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);
- 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);
- 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);
- 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);
- }
- #what{
- font-size:50pt;
- font-family:Verdana
- }
- #Button{
- margin:50px 0 0 0;
- }
- #Button input{
- font-size:30px;
- padding:0 ;
- background-color: #2ecc71;
- box-shadow: 0px 5px 0px 0px #15B358;
- outline:none;
- }
- #Button input:hover {
- background-color: #48E68B;
- outline:none;
- }
- #Button input:active {
- transform: translate(0px, 5px);
- -webkit-transform: translate(0px, 5px);
- box-shadow: 0px 1px 0px 0px;
- outline:none;
- }
- #start{
- background-color:#40AA53;
- border:1px solid #40AA53;
- width:200px;
- height:65px;
- margin:0em auto;
- font-size:1em;
- border-radius:10px;
- color:#FFF;
- -webkit-appearance:none;
- -moz-appearance:none
- }
- #footer {
- margin-top: 100px;
- border-top: 1px solid #ccc;
- padding: 20px 0;
- color: #ccc;
- }
- #popbox-wrapper{
- position: absolute;
- top: 0;
- left: 0;
- display: none;
- width: 100%;
- height: 100%;
- }
- #popbox{
- position: absolute;
- top: 50%;
- left: 50%;
- margin-top: -140px;
- margin-left: -200px;
- width: 400px;
- height: 300px;
- -webkit-border-radius: 5px;
- -moz-border-radius: 5px;
- border-radius: 5px;
- background: #fff;
- -webkit-box-shadow: 0 0 0 5px rgba(0,0,0,.2);
- -moz-box-shadow: 0 0 0 5px rgba(0,0,0,.2);
- box-shadow: 0 0 0 5px rgba(0,0,0,.2);
- +border: 5px solid #5D5D5C;
- }
- .ie8 #popbox { border: 5px solid #5D5D5C }
- #popbox h3{
- margin: 0;
- padding: 20px 0;
- border-bottom: 1px solid #bbb;
- -webkit-border-radius: 5px 5px 0 0;
- -moz-border-radius: 5px 5px 0 0;
- border-radius: 5px 5px 0 0;
- background: #E7E7E7 url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAA7CAIAAAA1lncVAAAAIklEQVQI12P4//8/0////5kYGBjgNC42PnWkilHDDBLtAgC6y0sq4aOFWQAAAABJRU5ErkJggg==") repeat-x 0 100%;
- color: #333;
- text-align: center;
- font-weight: 700;
- font-size: 19px;
- line-height: 1;
- }
- #popbox textarea{
- overflow: auto;
- margin: 10px;
- width: 380px;
- height: 160px;
- border: 0;
- background: #efefef;
- font-size: 14px;
- resize: none;
- padding: 10px;
- }
- #cfg {
- color: #666;
- cursor: pointer;
- }
- #cfg:hover {
- font-weight: 700;
- }
- .temp{
- position: absolute;
- z-index: -1;
- color: #777;
- }
Demo:http://garden.haoht123.com/food
感谢
本文基于小蝴蝶的基础上添加了可以自定义候选菜单的功能以及一些效果。
小蝴蝶 :今晚吃啥呢?——随机摇饭店前后端代码
完。
微信小程序
互联网开发,终身学习者,欢迎您的关注!
2018年8月16日 上午9:31
…… 我都想吃呢[帅吗?]
2018年8月16日 上午9:46
@云柯博客 一个一个来
2018年8月2日 下午9:27
这才是生活,想吃啥就吃啥,而我是生存,想吃什么都要先看看口袋,然后还是喝白粥吧
2018年8月2日 下午9:35
@懿古今 你是以后的我。我现在是一人吃饱,全家不饿
2018年8月1日 上午11:27
选择困难症人的终极福利
2018年8月1日 上午11:59
@Roogle