Thursday 15 May 2014

random - Jquery on page load show div and hide another -



random - Jquery on page load show div and hide another -

i trying create effect this: every time open page of site appears div while div disappears , , alternated . div1 , div2 , div1 , div2 .... managed random , in code below , switch order every page loads, refreshs. help me these?

jquery("document").ready(function($){ var numrand = math.floor(math.random() * 2); switch (numrand) { case 0: $(".div1").remove(); break; case 1: $(".div2").remove(); break; } });

you're going have store chosen lastly time somewhere. cookie may easiest way.

code doesn't run on because of cross-domain issues, here's working jsfiddle

class="snippet-code-js lang-js prettyprint-override">$(function() { var numrand = getcookievalue("numrand") || 0; switch (numrand % 2) { case 0: $(".div1").remove(); break; case 1: $(".div2").remove(); break; } setcookievalue("numrand", ++numrand); }); function getcookievalue(key) { var value = null; var cookiearray = document.cookie.split(';'); for (var = 0; < cookiearray.length; i++) { var keyvaluepair = cookiearray[i].split("="); if (keyvaluepair[0] == key) { value = keyvaluepair[1]; break; } } return value; } function setcookievalue(key, value) { document.cookie = key + "=" + value; } class="snippet-code-html lang-html prettyprint-override"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="div1"> div1 </div> <div class="div2"> div2 </div>

jquery random load switch-statement refresh

No comments:

Post a Comment