【问题标题】:HTML, CSS image sliderHTML、CSS 图像滑块
【发布时间】:2019-11-19 09:38:45
【问题描述】:

我认为 CSS 有问题。我会告诉你我的代码。它应该是这样的:

Should look like this

但在我的代码中是这样的:

On my page

我的代码有什么问题?

我也想有无限的图片滑块,我可以无限次按下和前进,幻灯片应该一直滑动图片。

非常感谢您的帮助。

我在 Windows 10 x64 Home 上,我使用 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>

【问题讨论】:

  • 请尝试在 Snippet 中重现此问题。

标签: javascript html css slider


【解决方案1】:

在JS中你有

document.querySelector('#gallery')

但是在 HTML 中你没有带有 id 属性的 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>

【讨论】:

  • 我复制了您编写的所有代码,但我仍然得到相同的结果。它看起来不像来自 sn-p 的代码:/ ibb.co/mSc0Q4b
  • 您可能有其他 CSS 规则覆盖图像查看器的规则。查看this
猜你喜欢
  • 2021-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多