sách gpt4 ai đã đi

javascript - 将图像动态添加到这个漂亮的框架中

In lại 作者:行者123 更新时间:2023-12-01 15:24:46 26 4
mua khóa gpt4 Nike

这是一个用纯 CSS 制作的精美动画边框。

如果我们想要这个动画边框作为一个框架并添加一个图像作为其中的内容。

当我添加图像时,它只是覆盖了框架,但我想将图像放在框架内,而不是在框架上。

我已经尽力了,但没有手我找不到解决方案。

这是我尝试过的(我需要使用javascript动态添加图像):

//addimage(); // uncomment this to add the image to the frame
function addimage(){

let pictureSource = '../../../wp-content/uploads/2013/03/ArchitectureArtdesigns-8.jpg';

let image = document.createElement('img');
image.setAttribute("id", "shot");
var node = document.getElementsByClassName('content');
node[0].appendChild(image);
image.src = `${pictureSource}`;
let shot = document.getElementById("shot");

}
html,
body,
.box .content {
height: 100%;
hiển thị: flex;
align-items: center;
justify-content: center;
background-color: black;
}

.box {
width: 300px;
height: 300px;
box-sizing: border-box;
padding: 15px;
vị trí: tương đối;
overflow: hidden;
}

.box::before {
content: '';
vị trí: tuyệt đối;
width: 150%;
height: 150%;
background: repeating-linear-gradient(
white 0%,
white 7.5px,
hotpink 7.5px,
hotpink 15px,
white 15px,
white 22.5px,
hotpink 22.5px,
hotpink 30px);
transform: translateX(-20%) translateY(-20%) rotate(-45deg);
animation: animate 20s linear infinite;
}

.box .content {
vị trí: tương đối;
background-color: white;
flex-direction: cột;
box-sizing: border-box;
padding: 30px;
căn chỉnh văn bản: ở giữa;
font-family: sans-serif;
z-index: 2;
}

.box,
.box .content {
box-shadow: 0 0 2px deeppink,
0 0 5px rgba(0, 0, 0, 1),
inset 0 0 5px rgba(0, 0, 0, 1);
border-radius: 10px;
}

.box .content h2 {
color: deeppink;
}

.box .content p {
color: dimgray;
}

@keyframes animate {
from {
background-position: 0;
}

to {
background-position: 0 450px;
}
}




1 Câu trả lời

将其添加为内容背景:

addimage(); // uncomment this to add the image to the frame
function addimage(){

let pictureSource = '../../../wp-content/uploads/2013/03/ArchitectureArtdesigns-8.jpg';

var node = document.getElementsByClassName('content');
node[0].style.background='url("'+pictureSource+'") center/cover'

}
html,
body,
.box .content {
height: 100%;
hiển thị: flex;
align-items: center;
justify-content: center;
background-color: black;
}

.box {
width: 300px;
height: 300px;
box-sizing: border-box;
padding: 15px;
vị trí: tương đối;
overflow: hidden;
}

.box::before {
content: '';
vị trí: tuyệt đối;
width: 150%;
height: 150%;
background: repeating-linear-gradient(
white 0%,
white 7.5px,
hotpink 7.5px,
hotpink 15px,
white 15px,
white 22.5px,
hotpink 22.5px,
hotpink 30px);
transform: translateX(-20%) translateY(-20%) rotate(-45deg);
animation: animate 20s linear infinite;
}

.box .content {
vị trí: tương đối;
background-color: white;
flex-direction: cột;
box-sizing: border-box;
padding: 30px;
căn chỉnh văn bản: ở giữa;
font-family: sans-serif;
z-index: 2;
}

.box,
.box .content {
box-shadow: 0 0 2px deeppink,
0 0 5px rgba(0, 0, 0, 1),
inset 0 0 5px rgba(0, 0, 0, 1);
border-radius: 10px;
}

.box .content h2 {
color: deeppink;
}

.box .content p {
color: dimgray;
}

@keyframes animate {
from {
background-position: 0;
}

to {
background-position: 0 450px;
}
}






如果你愿意,你可以简化动画的代码,如下所示:

addimage(); // uncomment this to add the image to the frame
function addimage() {

let pictureSource = '../../../wp-content/uploads/2013/03/ArchitectureArtdesigns-8.jpg';

var node = document.getElementsByClassName('content');
node[0].style.background = 'url("' + pictureSource + '") center/cover'

}
thân hình {
height: 100vh;
margin:0;
hiển thị: flex;
align-items: center;
justify-content: center;
background-color: black;
}

.box {
border-radius: 10px;
background:
repeating-linear-gradient(-45deg, white 0 7.5px, hotpink 0 15px)
0 0/21.21px 21.21px; /* 21.21px = sqrt(2) * 15px */
animation: animate 1s linear infinite;
}

.box .content {
width: 300px;
height: 300px;
border-radius: 10px;
box-shadow: 0 0 2px deeppink, 0 0 5px rgba(0, 0, 0, 1), inset 0 0 5px rgba(0, 0, 0, 1);
margin:15px;
}


@keyframes animate {
to {
background-position: 21.21px 21.21px;
}
}




关于javascript - 将图像动态添加到这个漂亮的框架中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60243135/

26 4 0
Bài viết được đề xuất: javascript - Sucrase 只移动 JS 文件
Bài viết được đề xuất: (Java) Java không tìm thấy tệp hình ảnh của tôi
Bài viết được đề xuất: java - 内存游戏java
Bài viết được đề xuất: javascript - 对象作为 JS Map 数据类型中的键的实际用途是什么?
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com