将 JSfiddle 转换为实际网站 - 随机图像生成器网站
Transform JSfiddle to actual website - random image generator website
您好,我想创建一个随机图像生成器网站,最近在网上找到了一个符合我意图的 JSfiddle 文件。我想使用 CSS 和 JS 将该 JSfiddle 文件传输到 HTML。当我在 JSfiddle 中 运行 它工作正常但是当我将它应用到实际网站时它只识别 HTML 和 CSS 而不是 JS,这意味着它看起来不错但不t 生成随机图像。我对编码有点陌生,不知道出了什么问题,请查看下面的编码。非常感谢。
HTML
<!DOCTYPE html>
<html>
<head>
<title>Random</title>
<link rel="stylesheet" type="text/css" href="main.css">
<script src="style.js"></script>
</head>
<body>
<div id="banner-message">
<p>Hello World</p>
<button id="button">Change image</button>
</div>
<div id="img"></div>
</body>
</html>
CSS
*{
margin: 0;
padding: 0;
}
header{
background-image:linear-gradient(rgba(0,0,0,0.8),rgba(0,0,0,0.8)), url(../Arcane/Images/thefoollabel.png);
height: 100vh;
background-size: cover;
background-position: center;
}
ul{
float: right;
list-style-type: none;
margin-top: 25px;
}
ul li{
display: inline-block;
}
ul li a{
text-decoration: none;
color: #fff;
padding: 5px 20px;
border: 1px solid transparent;
transition: 0.4s ease;
}
ul li a:hover{
background-color: #fff;
color: black;
}
ul li.active a{
background-color: #fff;
color: black;
}
.logo img{
float: left;
width: 150px;
height: auto;
margin-top: 20px;
}
.main{
max-width: 1200px;
margin: auto;
}
.title{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
.title h1{
font-size: 70px;
color: #fff;
}
.subtitle{
position: absolute;
text-align: center;
top: 60%;
left: 50%;
transform: translate(-50%,-50%);
}
.subtitle h1{
font-family: Helvetica;
font-weight: 500;
font-size: 20px;
color: #bebc53;
}
.subsubtitle{
position: absolute;
text-align: center;
top: 66%;
left: 50%;
transform: translate(-50%,-50%);
}
.subsubtitle h1{
font-family: Helvetica;
font-style: italic;
font-weight: 500;
font-size: 20px;
color: #bebc53;
}
.button{
position: absolute;
top: 75%;
left: 50%;
transform: translate(-50%,-50%);
}
.btn{
border: 1px solid #fff;
padding: 10px 30px;
color: #fff;
font-size: 20px;
text-decoration: none;
transition: 0.4s ease;
}
.btn:hover{
background-color: #fff;
color: black;
}
JS
var images =[
'https://i.ibb.co/37bntqy/img-2.png',
'https://i.ibb.co/yXcJBVg/img-1.png',
];
// Set default image for load
document.getElementById('img').style.backgroundImage = "url('" + images[0] + "')";
var button = document.getElementById("button").addEventListener("click", function(){
// handle click and change background image
var r = Math.floor(Math.random() * images.length);
document.getElementById('img').style.backgroundImage = "url('" + images[r] + "')";
});
JSFiddle 默认执行onload。您需要将 JS 包装在负载处理程序中
const images = [
'https://i.ibb.co/37bntqy/img-2.png',
'https://i.ibb.co/yXcJBVg/img-1.png',
];
window.addEventListener("load",function() {
document.getElementById('img').style.backgroundImage = "url('" + images[0] + "')";
document.getElementById("button").addEventListener("click", function(){
// handle click and change background image
var r = Math.floor(Math.random() * images.length);
document.getElementById('img').style.backgroundImage = "url('" + images[r] + "')";
});
})
此代码可以进入头部或外部 JS 文件和 main.css
中的 CSS
const images = [
'https://i.ibb.co/37bntqy/img-2.png',
'https://i.ibb.co/yXcJBVg/img-1.png',
];
window.addEventListener("load", function() {
document.getElementById('imgDiv').style.backgroundImage = "url('" + images[0] + "')";
document.getElementById("button").addEventListener("click", function() {
// handle click and change background image
var r = Math.floor(Math.random() * images.length);
document.getElementById('imgDiv').style.backgroundImage = "url('" + images[r] + "')";
});
})
* {
margin: 0;
padding: 0;
}
header{
background-image:linear-gradient(rgba(0,0,0,0.8),rgba(0,0,0,0.8)), url(../Arcane/Images/thefoollabel.png);
height: 100vh;
background-size: cover;
background-position: center;
}
ul {
float: right;
list-style-type: none;
margin-top: 25px;
}
ul li {
display: inline-block;
}
ul li a {
text-decoration: none;
color: #fff;
padding: 5px 20px;
border: 1px solid transparent;
transition: 0.4s ease;
}
ul li a:hover {
background-color: #fff;
color: black;
}
#imgDiv { min-height: 1000px; }
<div id="banner-message">
<p>Hello World</p>
<button id="button">Change image</button>
</div>
<div id="imgDiv">Test
</div>
欢迎来到 Whosebug,
代码是正确的,但您应该做的就是在正文末尾添加加载 script
文件的 <script>
标记,javascript 无法获取您的 img
div 因为 dom 元素尚未加载。
<script src="style.js"></script>
</body>
您好,我想创建一个随机图像生成器网站,最近在网上找到了一个符合我意图的 JSfiddle 文件。我想使用 CSS 和 JS 将该 JSfiddle 文件传输到 HTML。当我在 JSfiddle 中 运行 它工作正常但是当我将它应用到实际网站时它只识别 HTML 和 CSS 而不是 JS,这意味着它看起来不错但不t 生成随机图像。我对编码有点陌生,不知道出了什么问题,请查看下面的编码。非常感谢。
HTML
<!DOCTYPE html>
<html>
<head>
<title>Random</title>
<link rel="stylesheet" type="text/css" href="main.css">
<script src="style.js"></script>
</head>
<body>
<div id="banner-message">
<p>Hello World</p>
<button id="button">Change image</button>
</div>
<div id="img"></div>
</body>
</html>
CSS
*{
margin: 0;
padding: 0;
}
header{
background-image:linear-gradient(rgba(0,0,0,0.8),rgba(0,0,0,0.8)), url(../Arcane/Images/thefoollabel.png);
height: 100vh;
background-size: cover;
background-position: center;
}
ul{
float: right;
list-style-type: none;
margin-top: 25px;
}
ul li{
display: inline-block;
}
ul li a{
text-decoration: none;
color: #fff;
padding: 5px 20px;
border: 1px solid transparent;
transition: 0.4s ease;
}
ul li a:hover{
background-color: #fff;
color: black;
}
ul li.active a{
background-color: #fff;
color: black;
}
.logo img{
float: left;
width: 150px;
height: auto;
margin-top: 20px;
}
.main{
max-width: 1200px;
margin: auto;
}
.title{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
.title h1{
font-size: 70px;
color: #fff;
}
.subtitle{
position: absolute;
text-align: center;
top: 60%;
left: 50%;
transform: translate(-50%,-50%);
}
.subtitle h1{
font-family: Helvetica;
font-weight: 500;
font-size: 20px;
color: #bebc53;
}
.subsubtitle{
position: absolute;
text-align: center;
top: 66%;
left: 50%;
transform: translate(-50%,-50%);
}
.subsubtitle h1{
font-family: Helvetica;
font-style: italic;
font-weight: 500;
font-size: 20px;
color: #bebc53;
}
.button{
position: absolute;
top: 75%;
left: 50%;
transform: translate(-50%,-50%);
}
.btn{
border: 1px solid #fff;
padding: 10px 30px;
color: #fff;
font-size: 20px;
text-decoration: none;
transition: 0.4s ease;
}
.btn:hover{
background-color: #fff;
color: black;
}
JS
var images =[
'https://i.ibb.co/37bntqy/img-2.png',
'https://i.ibb.co/yXcJBVg/img-1.png',
];
// Set default image for load
document.getElementById('img').style.backgroundImage = "url('" + images[0] + "')";
var button = document.getElementById("button").addEventListener("click", function(){
// handle click and change background image
var r = Math.floor(Math.random() * images.length);
document.getElementById('img').style.backgroundImage = "url('" + images[r] + "')";
});
JSFiddle 默认执行onload。您需要将 JS 包装在负载处理程序中
const images = [
'https://i.ibb.co/37bntqy/img-2.png',
'https://i.ibb.co/yXcJBVg/img-1.png',
];
window.addEventListener("load",function() {
document.getElementById('img').style.backgroundImage = "url('" + images[0] + "')";
document.getElementById("button").addEventListener("click", function(){
// handle click and change background image
var r = Math.floor(Math.random() * images.length);
document.getElementById('img').style.backgroundImage = "url('" + images[r] + "')";
});
})
此代码可以进入头部或外部 JS 文件和 main.css
中的 CSSconst images = [
'https://i.ibb.co/37bntqy/img-2.png',
'https://i.ibb.co/yXcJBVg/img-1.png',
];
window.addEventListener("load", function() {
document.getElementById('imgDiv').style.backgroundImage = "url('" + images[0] + "')";
document.getElementById("button").addEventListener("click", function() {
// handle click and change background image
var r = Math.floor(Math.random() * images.length);
document.getElementById('imgDiv').style.backgroundImage = "url('" + images[r] + "')";
});
})
* {
margin: 0;
padding: 0;
}
header{
background-image:linear-gradient(rgba(0,0,0,0.8),rgba(0,0,0,0.8)), url(../Arcane/Images/thefoollabel.png);
height: 100vh;
background-size: cover;
background-position: center;
}
ul {
float: right;
list-style-type: none;
margin-top: 25px;
}
ul li {
display: inline-block;
}
ul li a {
text-decoration: none;
color: #fff;
padding: 5px 20px;
border: 1px solid transparent;
transition: 0.4s ease;
}
ul li a:hover {
background-color: #fff;
color: black;
}
#imgDiv { min-height: 1000px; }
<div id="banner-message">
<p>Hello World</p>
<button id="button">Change image</button>
</div>
<div id="imgDiv">Test
</div>
欢迎来到 Whosebug,
代码是正确的,但您应该做的就是在正文末尾添加加载 script
文件的 <script>
标记,javascript 无法获取您的 img
div 因为 dom 元素尚未加载。
<script src="style.js"></script>
</body>