Как по кнопке, один див поменять другим
html {
width: 100%;
height: 100%;
}
body {
height: 100vh;
margin: 0;
padding: 0;
min-height: 100vh;
display: flex;
flex-direction: column;
background-image: url(background.png);
background-size: cover;
}
.header {
background-color: black;
width: 100%;
min-height: 100px;
margin: 0%;
}
.container {
height: 90px;
width: 90px;
position: relative;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.outer {
background-image: url("background.png");
background-repeat: no-repeat;
background-position: center center;
background-attachment: fixed;
background-size: cover;
}
.main {
position: relative;
display: block;
height: 100vh;
}
.container2 {
position: absolute;
width: 1200px;
height: 450px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.container3 {
float: left;
position: relative;
width: 1200px;
height: 450px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.discorddontclick1 {
display: inline-block;
width: 300px;
height: 450px;
border-radius: 50px;
background-color: black;
vertical-align: top;
margin-left: 50px;
}
.discorddontclick2 {
display: inline-block;
width: 300px;
height: 450px;
border-radius: 50px;
background-color: black;
vertical-align: top;
margin-left: 100px;
}
.discorddontclick3 {
display: inline-block;
width: 300px;
height: 450px;
border-radius: 50px;
background-color: black;
vertical-align: top;
margin-left: 100px;
}
.dslogo {
display: block;
width: 250px;
height: 250px;
background-image: url(dsLogo.png);
margin-left: 25px;
}
.serverlogo {
display: block;
width: 250px;
height: 250px;
background-image: url(severlogo.png);
margin-left: 25px;
}
.sociallogo {
display: block;
width: 250px;
height: 250px;
background-image: url(sociallogo.png);
margin-left: 25px;
}
.text1 {
display: block;
width: 253px;
height: 31px;
margin-left: 23.5px;
}
.ds {
display: flex;
color: aliceblue;
margin: 0;
font-family: 'Joan', sans-serif;
font-size: 24px;
padding: 0;
}
.text2 {
display: block;
width: 274px;
height: 31px;
margin-left: 20px;
text-align: center;
}
.servera {
display: flex;
color: aliceblue;
margin: 0;
font-family: 'Joan', sans-serif;
font-size: 24px;
padding: 0;
text-align: center;
}
.text3 {
display: block;
width: 178px;
height: 31px;
margin-left: 61px;
text-align: center;
}
.medialink {
display: flex;
color: aliceblue;
margin: 0;
font-family: 'Joan', sans-serif;
font-size: 24px;
padding: 0;
text-align: center;
}
.buttongradient {
width: 200px;
height: 80px;
background-image: url(gradient.png);
margin-left: 50px;
margin-top: 50px;
}
.moreinf1 {
width: 200px;
height: 80px;
border-radius: 50px;
background-color: transparent;
background-repeat: no-repeat;
border: none;
cursor: pointer;
outline: none;
font-family: 'Joan', sans-serif;
font-size: 30px;
font-weight: 400;
line-height: 39px;
letter-spacing: 0em;
text-align: center;
color: aliceblue;
}
.buttongradient2 {
width: 200px;
height: 80px;
background-image: url(gradient.png);
margin-left: 50px;
margin-top: 50px;
}
.moreinf2 {
width: 200px;
height: 80px;
border-radius: 50px;
background-color: transparent;
background-repeat: no-repeat;
border: none;
cursor: pointer;
outline: none;
font-family: 'Joan', sans-serif;
font-size: 30px;
font-weight: 400;
line-height: 39px;
letter-spacing: 0em;
text-align: center;
color: aliceblue;
}
.buttongradient3 {
width: 200px;
height: 80px;
background-image: url(gradient.png);
margin-left: 50px;
margin-top: 50px;
}
.moreinf3 {
width: 200px;
height: 80px;
border-radius: 50px;
background-color: transparent;
background-repeat: no-repeat;
border: none;
cursor: pointer;
outline: none;
font-family: 'Joan', sans-serif;
font-size: 30px;
font-weight: 400;
line-height: 39px;
letter-spacing: 0em;
text-align: center;
color: aliceblue;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Destiny Project</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Joan&display=swap" rel="stylesheet">
</head>
<body>
<div class="header">
<div class="container">
<a href="page1.html">
<img src="logo1.png" alt="">
</a>
</div>
</div>
<div class="main">
<div class="container2">
<div class="container3">
<div class="discorddontclick1">
<div class="dslogo"></div>
<div class="text1">
<p class="ds">Наши Discord сервера</p>
</div>
<div class="buttongradient">
<button class="moreinf1">Подробнее</button>
</div>
</div>
<div class="discorddontclick2">
<div class="serverlogo"></div>
<div class="text2">
<p class="servera">Наши игровые сервера</p>
</div>
<div class="buttongradient2">
<button class="moreinf2">Подробнее</button>
</div>
</div>
<div class="discorddontclick3">
<div class="sociallogo"></div>
<div class="text3">
<p class="medialink">Мы в соц.сетях</p>
</div>
<div class="buttongradient3">
<button class="moreinf3">Подробнее</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

У меня есть такой див, который при нажатии на кнопку подробнее, должен становиться больше и становиться таким
Ответы (1 шт):
Автор решения: SwaD
→ Ссылка
Что бы заменить один div другим по кнопке, можно, допустим использовать свойство innerHTML. Оно хорошо подходит для статических элементов
const div = document.getElementById('container');
const btn = document.getElementById('btn');
const block1 = '<div>Первый</div>';
const block2 = '<div>Второй</div>';
let idBlock = 1;
btn.addEventListener('click', () => {
idBlock++;
div.innerHTML = idBlock % 2 ? block1 : block2;
})
div.innerHTML = block1;
<div id="container"></div><br/>
<button id="btn">Сменить</button>
Если надо менять html элементы, которые обладают своими собственными событиями, то их надо сначала поместить в переменную(в коде ниже, создается кнопка из функции)
const btnContainer = document.getElementById('buttons');
const add = document.getElementById('addBtn');
let buttonNumber = 1;
function buildButton() {
const button = document.createElement('button');
const name = buttonNumber++;
button.innerText = name;
button.addEventListener('click', () => console.log('click', name))
return button;
}
let prev = buildButton()
add.addEventListener('click', () => {
const newBtn = buildButton()
btnContainer.replaceChild(newBtn, prev)
prev = newBtn;
})
btnContainer.appendChild(prev)
<div id="buttons"></div>
<button id="addBtn">Сменить кнопку</button>