简体   繁体   English

我有代码可以在javascript和CSS中刷新图像时将图像随机化,以格式化图像,但我不知道如何将两者结合起来

[英]I've got code to randomize an image on refresh in javascript and CSS to format the image but I don't know how to combine the two

I'm sorry if the title doesn't make sense, I'm extremely new to this, so here's a jsfiddle of what I'm talking about: https://jsfiddle.net/a3668a1f/1/ 很抱歉,如果标题没有意义,我对此是个新手,所以这是我所谈论的内容: https ://jsfiddle.net/a3668a1f/1/

I want the images that show up below the title box to appear in the title box. 我希望显示在标题框下方的图像出现在标题框中。

I just threw those images in there as an example. 我只是将这些图像放在那里作为示例。

 var images = [ "https://i.redd.it/1ev9elr85xcz.jpg", "https://i.redd.it/g6kc3upsvqdz.jpg", "https://i.redd.it/koucki3aygdz.jpg" ]; function randImg() { var size = images.length var x = Math.floor(size * Math.random()) document.getElementById('image').src = images[x]; } randImg(); 
 h1 { color: silver; text-align: center; max-width: 100%; vertical-align: middle; line-height: 85px; height: 100px; border: solid; border-color: silver; border-radius: 25px; border-width: 5px; background-position: center; background-repeat: no-repeat; background-image: url(https://i.redd.it/pvqzvajftpdz.jpg); background-size: cover; box-sizing: border-box; } 
 <div> <h1> Title. <img id="image" /> </h1> </div> 

Do you mean this? 你是这个意思吗

 var images = [ "https://i.redd.it/1ev9elr85xcz.jpg", "https://i.redd.it/g6kc3upsvqdz.jpg", "https://i.redd.it/koucki3aygdz.jpg" ]; function randImg() { var size = images.length var x = Math.floor(size * Math.random()) document.querySelector('h1').style.backgroundImage='url('+images[x]+')'; } randImg(); 
 h1 { color: silver; text-align: center; max-width: 100%; vertical-align: middle; line-height: 85px; height: 100px; border: solid; border-color: silver; border-radius: 25px; border-width: 5px; background-position: center; background-repeat: no-repeat; background-image: url(https://i.redd.it/pvqzvajftpdz.jpg); background-size: cover; box-sizing: border-box; } 
 <div> <h1> Title. <img id="image" /> </h1> </div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我必须在移动浏览器上显示1000多个未分类的图片缩略图,但我不知道如何 - I've got to display 1000+ uncategorized picture thumbnails on a mobile browser and I don't know how 我正在努力在 javascript 中追加行,但我实际上不知道如何在行单元格中添加 append 图像 - I am working on appending row in javascript but I actually don't know how to append an image in row cell 我不想将一串数字格式化为 javascript 中的货币,但我不知道如何格式化我的字符串 - I wan't to format a string of numbers as a currency in javascript but i don't know how to format my string 如何每天而不是每次刷新页面时随机化背景图像 - How can i randomize background image on daily basis rather than everytime you refresh the page 在Angular中,不知道扩展名时如何加载图像? - In Angular, how do I load an image when I don't know the extension? 如何使用 html、css 和 javascript 随机化滑块画廊中的图像? - How to randomize an image within a slidershow gallery with html, css, and javascript? Javascript / CSS幻灯片:使用透明胶片突出了我不知道如何解决的缺陷 - Javascript/CSS slideshow: Using transparencies highlights flaws I don't know how to fix 如何模糊图像的一部分 <img> 使用CSS标记,我没有使用背景图片的选项 - How to blur part of an image <img> tag using css, I don't have an option to use background image 如何让我的代码等到我从服务器获得处理过的图像以在 Javascript 中显示 - How can I make my code wait until I got processed image from the server to display in Javascript 我要让我的 javascript 从我的图像文件夹中保留一张随机图像,在写完所有代码后图像没有显示,我不知道为什么 - i am to make my javascript keep a random image from my image folder, after writing all the codes the image is not showing and i don't know why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM