gpt4 book ai didi

javascript - HTML、CSS 图像 slider

转载 作者:行者123 更新时间:2023-11-28 12:28:35 39 4
gpt4 key购买 nike

我觉得 CSS 有问题。我会告诉你我的代码。它应该看起来像这样:

Should look like this

但在我的代码中它看起来像这样:

On my page

我的代码有什么问题?

我还想为图片设置无限 slider ,我可以无限次按下并向前移动,幻灯片应该一直滑动图片。

非常感谢您的帮助。

我在 Windows 10 x64 家庭版上使用 VS Code 1.40。

JS

var gallery = document.querySelector('#gallery');
var getVal = function (elem, style) { return parseInt(window.getComputedStyle(elem).getPropertyValue(style)); };
var getHeight = function (item) { return item.querySelector('.content').getBoundingClientRect().height; };
var resizeAll = function () {
var altura = getVal(gallery, 'grid-auto-rows');
var gap = getVal(gallery, 'grid-row-gap');
gallery.querySelectorAll('.gallery-item').forEach(function (item) {
var el = item;
el.style.gridRowEnd = "span " + Math.ceil((getHeight(item) + gap) / (altura + gap));
});
};
gallery.querySelectorAll('img').forEach(function (item) {
item.classList.add('byebye');
if (item.complete) {
console.log(item.src);
}
else {
item.addEventListener('load', function () {
var altura = getVal(gallery, 'grid-auto-rows');
var gap = getVal(gallery, 'grid-row-gap');
var gitem = item.parentElement.parentElement;
gitem.style.gridRowEnd = "span " + Math.ceil((getHeight(gitem) + gap) / (altura + gap));
item.classList.remove('byebye');
});
}
});
window.addEventListener('resize', resizeAll);
gallery.querySelectorAll('.gallery-item').forEach(function (item) {
item.addEventListener('click', function () {
item.classList.toggle('full');
});
});
body {
background-color: #eee;
}
.hello {
opacity: 1 !important;
}
.full {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
z-index: 1;
}
.full .content {
background-color: rgba(0,0,0,0.75) !important;
height: 100%;
width: 100%;
display: grid;
}
.full .content img {
left: 50%;
transform: translate3d(0, 0, 0);
animation: zoomin 1s ease;
max-width: 100%;
max-height: 100%;
margin: auto;
}
.byebye {
opacity: 0;
}
.byebye:hover {
transform: scale(0.2) !important;
}
.gallery {
display: grid;
grid-column-gap: 8px;
grid-row-gap: 8px;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
grid-auto-rows: 8px;
}
.gallery img {
max-width: 100%;
border-radius: 8px;
box-shadow: 0 0 16px #333;
transition: all 1.5s ease;
}
.gallery img:hover {
box-shadow: 0 0 32px #333;
}
.gallery .content {
padding: 4px;
}
.gallery .gallery-item {
transition: grid-row-start 300ms linear;
transition: transform 300ms ease;
transition: all 0.5s ease;
cursor: pointer;
}
.gallery .gallery-item:hover {
transform: scale(1.025);
}
@media (max-width: 600px) {
.gallery {
grid-template-columns: repeat(auto-fill, minmax(30%, 1fr));
}
}
@media (max-width: 400px) {
.gallery {
grid-template-columns: repeat(auto-fill, minmax(50%, 1fr));
}
}
@-moz-keyframes zoomin {
0% {
max-width: 50%;
transform: rotate(-30deg);
filter: blur(4px);
}
30% {
filter: blur(4px);
transform: rotate(-80deg);
}
70% {
max-width: 50%;
transform: rotate(45deg);
}
100% {
max-width: 100%;
transform: rotate(0deg);
}
}
@-webkit-keyframes zoomin {
0% {
max-width: 50%;
transform: rotate(-30deg);
filter: blur(4px);
}
30% {
filter: blur(4px);
transform: rotate(-80deg);
}
70% {
max-width: 50%;
transform: rotate(45deg);
}
100% {
max-width: 100%;
transform: rotate(0deg);
}
}
@-o-keyframes zoomin {
0% {
max-width: 50%;
transform: rotate(-30deg);
filter: blur(4px);
}
30% {
filter: blur(4px);
transform: rotate(-80deg);
}
70% {
max-width: 50%;
transform: rotate(45deg);
}
100% {
max-width: 100%;
transform: rotate(0deg);
}
}
@keyframes zoomin {
0% {
max-width: 50%;
transform: rotate(-30deg);
filter: blur(4px);
}
30% {
filter: blur(4px);
transform: rotate(-80deg);
}
70% {
max-width: 50%;
transform: rotate(45deg);
}
100% {
max-width: 100%;
transform: rotate(0deg);
}
}
<!DOCTYPE html>
<body>
<div class="slider" id="slider">
<div class="slider-item">
<div class="content"><img src="assets/slider-image-1.jpg"></div>
</div>
<div class="slider-item">
<div class="content"><img src="assets/slider-image-2.jpg"></div>
</div>
<div class="slider-item">
<div class="content"><img src="assets/slider-image-3.jpg"></div>
</div>
<div class="slider-item">
<div class="content"><img src="assets/slider-image-4.jpg"></div>
</div>
<div class="slider-item">
<div class="content"><img src="assets/slider-image-5.jpg"></div>
</div>
<div class="slider-item">
<div class="content"><img src="assets/slider-image-6.jpg"></div>
</div>
<div class="slider-item">
<div class="content"><img src="assets/slider-image-7.jpg"></div>
</div>
<div class="slider-item">
<div class="content"><img src="assets/slider-image-8.jpg"></div>
</div>
<div class="slider-item">
<div class="content"><img src="assets/slider-image-9.jpg"></div>
</div>
</div>
</body>
</html>

最佳答案

在 JS 中你有

document.querySelector('#gallery')

但是在 HTML 中你没有带有 id 属性 gallery 的 div。

此外,在 CSS 中,您正在为画廊使用类选择器,而在 HTML 中,没有元素具有画廊的类属性。

此外,当您在 CSS 中使用 gallery-item 类时,您的 HTML 元素具有 slider-item 的类属性。

var getVal = function(elem, style) {
return parseInt(window.getComputedStyle(elem).getPropertyValue(style));
};
var getHeight = function(item) {
return item.querySelector('.content').getBoundingClientRect().height;
};
var resizeAll = function() {
var altura = getVal(gallery, 'grid-auto-rows');
var gap = getVal(gallery, 'grid-row-gap');
gallery.querySelectorAll('.gallery-item').forEach(function(item) {
var el = item;
el.style.gridRowEnd = "span " + Math.ceil((getHeight(item) + gap) / (altura + gap));
});
};
gallery.querySelectorAll('img').forEach(function(item) {
item.classList.add('byebye');
if (item.complete) {
console.log(item.src);
} else {
item.addEventListener('load', function() {
var altura = getVal(gallery, 'grid-auto-rows');
var gap = getVal(gallery, 'grid-row-gap');
var gitem = item.parentElement.parentElement;
gitem.style.gridRowEnd = "span " + Math.ceil((getHeight(gitem) + gap) / (altura + gap));
item.classList.remove('byebye');
});
}
});
window.addEventListener('resize', resizeAll);
gallery.querySelectorAll('.gallery-item').forEach(function(item) {
item.addEventListener('click', function() {
item.classList.toggle('full');
});
});
body {
background-color: #eee;
}

.hello {
opacity: 1 !important;
}

.full {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
z-index: 1;
}

.full .content {
background-color: rgba(0, 0, 0, 0.75) !important;
height: 100%;
width: 100%;
display: grid;
}

.full .content img {
left: 50%;
transform: translate3d(0, 0, 0);
animation: zoomin 1s ease;
max-width: 100%;
max-height: 100%;
margin: auto;
}

.byebye {
opacity: 0;
}

.byebye:hover {
transform: scale(0.2) !important;
}

.gallery {
display: grid;
grid-column-gap: 8px;
grid-row-gap: 8px;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
grid-auto-rows: 8px;
}

.gallery img {
max-width: 100%;
border-radius: 8px;
box-shadow: 0 0 16px #333;
transition: all 1.5s ease;
}

.gallery img:hover {
box-shadow: 0 0 32px #333;
}

.gallery .content {
padding: 4px;
}

.gallery .gallery-item {
transition: grid-row-start 300ms linear;
transition: transform 300ms ease;
transition: all 0.5s ease;
cursor: pointer;
}

.gallery .gallery-item:hover {
transform: scale(1.025);
}

@media (max-width: 600px) {
.gallery {
grid-template-columns: repeat(auto-fill, minmax(30%, 1fr));
}
}

@media (max-width: 400px) {
.gallery {
grid-template-columns: repeat(auto-fill, minmax(50%, 1fr));
}
}

@-moz-keyframes zoomin {
0% {
max-width: 50%;
transform: rotate(-30deg);
filter: blur(4px);
}
30% {
filter: blur(4px);
transform: rotate(-80deg);
}
70% {
max-width: 50%;
transform: rotate(45deg);
}
100% {
max-width: 100%;
transform: rotate(0deg);
}
}

@-webkit-keyframes zoomin {
0% {
max-width: 50%;
transform: rotate(-30deg);
filter: blur(4px);
}
30% {
filter: blur(4px);
transform: rotate(-80deg);
}
70% {
max-width: 50%;
transform: rotate(45deg);
}
100% {
max-width: 100%;
transform: rotate(0deg);
}
}

@-o-keyframes zoomin {
0% {
max-width: 50%;
transform: rotate(-30deg);
filter: blur(4px);
}
30% {
filter: blur(4px);
transform: rotate(-80deg);
}
70% {
max-width: 50%;
transform: rotate(45deg);
}
100% {
max-width: 100%;
transform: rotate(0deg);
}
}

@keyframes zoomin {
0% {
max-width: 50%;
transform: rotate(-30deg);
filter: blur(4px);
}
30% {
filter: blur(4px);
transform: rotate(-80deg);
}
70% {
max-width: 50%;
transform: rotate(45deg);
}
100% {
max-width: 100%;
transform: rotate(0deg);
}
}
<!DOCTYPE html>

<body>
<div class="gallery" id="gallery">
<div class="gallery-item">
<div class="content"><img src="http://lorempixel.com/600/200/sports"></div>
</div>
<div class="gallery-item">
<div class="content"><img src="http://lorempixel.com/400/250/sports"></div>
</div>
<div class="gallery-item">
<div class="content"><img src="http://lorempixel.com/400/300/sports"></div>
</div>
<div class="gallery-item">
<div class="content"><img src="http://lorempixel.com/400/200/sports"></div>
</div>
<div class="gallery-item">
<div class="content"><img src="http://lorempixel.com/400/240/sports"></div>
</div>
<div class="gallery-item">
<div class="content"><img src="http://lorempixel.com/800/200/sports"></div>
</div>
<div class="gallery-item">
<div class="content"><img src="http://lorempixel.com/450/200/sports"></div>
</div>
<div class="gallery-item">
<div class="content"><img src="http://lorempixel.com/400/210/sports"></div>
</div>
<div class="slider-item">
<div class="content"><img src="assets/slider-image-9.jpg"></div>
</div>
</div>
</body>

</html>

关于javascript - HTML、CSS 图像 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58930824/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com