javascript - Change to random background image and corresponding text on click of button? -
lets have amount of images want have backgrounds, full covered, on site.
to each background image there's specific text want shown in center of screen.
each background image , corresponding text should randomized click of button.
i don't javascript or jquery, seems solution. though, havn't found code problem. there code want?
thanks!
jsfiddle demo of website.
html:
<div id="background"> <div id="box"> <div class="title">title of website</div> <div class="text">text corresponding specfic background shown.</div> <div class="button"> <button type="button">randomizing button</button> </div> </div> </div> css:
html, body { height: 100%; } #background { height: 100%; } #box { background: #fff; width: 40%; min-width: 400px; height: 200px; margin-left: auto; margin-right: auto; top: 40%; position: relative; } .title { width: 100%; height: 50px; float: left; background: #e1e1e1; text-align: center; font-size: 24px; line-height:50px; } .text { background: #c8c8c8; width: 100%; height: 100px; float: left; text-align: center; font-size: 18px; position: relative; padding-top: 20px; } .button { width: 100%; height: 50px; float: left; text-align: center; background: #afafaf; }
i've updated jsfiddle here
html (just added ids)
<div id="background"> <div id="box"> <div class="title">title of website</div> <div class="text" id="text-box">text corresponding specfic background shown.</div> <div class="button"> <button type="button" id="my-button">randomizing button</button> </div> </div> </div> js
var mydata = { // create json images 1: { imageurl: "http://lorempixel.com/800/800/cats/1", text: "this text image 1" }, 2: { imageurl: "http://lorempixel.com/800/800/cats/2", text: "this text image 2" }, 3: { imageurl: "http://lorempixel.com/800/800/cats/3", text: "this text image 3" }, 4: { imageurl: "http://lorempixel.com/800/800/cats/4", text: "this text image 4" }, 5: { imageurl: "http://lorempixel.com/800/800/cats/5", text: "this text image 5" }, 6: { imageurl: "http://lorempixel.com/800/800/cats/6", text: "this text image 6" }, 7: { imageurl: "http://lorempixel.com/800/800/cats/7", text: "this text image 7" }, 8: { imageurl: "http://lorempixel.com/800/800/cats/8", text: "this text image 8" }, 9: { imageurl: "http://lorempixel.com/800/800/cats/9", text: "this text image 9" }, 10: { imageurl: "http://lorempixel.com/800/800/cats/10", text: "this text image 10" } }; function changeimage(){ var randomnumber = math.floor((math.random() * 10) + 1); //change 10 number of images in json document.getelementbyid("background").style.background = "url('"+mydata[randomnumber].imageurl+"')"; document.getelementbyid("text-box").innerhtml = mydata[randomnumber].text; } document.getelementbyid("my-button").addeventlistener("click",changeimage); //bind button changeimage function
Comments
Post a Comment