如何在没有jQuery的情况下制作滑块图像库

时间:2019-01-13 04:50:17

标签: javascript html css

我试图在不使用jquery的情况下制作滑块图像库。目前,滑块工作得非常好,但是我的工作方式是将图像作为背景,并采用div的整个宽度,这种外观在不同设备上看起来很奇怪。我希望它们像灯箱一样弹出页面,但不使用jquery,以便图像以其正常宽度和高度显示,而不是被“覆盖”作为填充div的背景。老实说,我无法在网上找到任何东西,所以我在这里发布此问题以查看是否有人可以提供帮助。任何反馈将不胜感激。

我意识到这些图片实际上并未显示,但这是因为我不知道如何在代码段工具中上传图片

ravel
// getting items from the DOM
const menuButton = document.querySelector(".menuButton");
const menu = document.querySelector(".menu");
const myPortrait = document.querySelector(".myPortrait");
const menuItems = document.querySelector(".menuItems");
const navItem = document.querySelectorAll(".navItem");
const menuWrapper = document.querySelectorAll(".menuWrapper");
const photography = document.querySelector("#photography");

// Setting Initial State of Menue
let showMenu = false;

menuButton.addEventListener("click", openMenu);

function openMenu() {
  if (!showMenu) {
    menuButton.classList.add("close");
    menu.classList.add("show");
    menuWrapper.forEach(item => item.classList.add("show"));
    myPortrait.classList.add("show");
    menuItems.classList.add("show");
    navItem.forEach(item => item.classList.add("show"));
    photography.classList.add("show");

    //new menue state
    showMenu = true;
  } else {
    menuButton.classList.remove("close");
    menu.classList.remove("show");
    menuWrapper.forEach(item => item.classList.remove("show"));
    myPortrait.classList.remove("show");
    menuItems.classList.remove("show");
    navItem.forEach(item => item.classList.remove("show"));
    photography.classList.remove("show");

    //new menue state
    showMenu = false;
  }
}
//------------------------photography slide show---------------------------------
let slider = document.querySelectorAll(".slide");
leftArrow = document.querySelector(".leftArrow");
rightArrow = document.querySelector(".rightArrow");
current = 0;

//clears images
function reset() {
  for (let i = 0; i < slider.length; i++) {
    slider[i].style.display = "none";
  }
}
// starts slider
function startSlide() {
  reset();
  slider[0].style.display = "block";
}
// show slide left
function slideLeft() {
  reset();
  slider[current - 1].style.display = "block";
  current--;
}
// show slide right
function slideright() {
  reset();
  slider[current + 1].style.display = "block";
  current++;
}
//left arrow click
leftArrow.addEventListener("click", function() {
  if (current === 0) {
    current = slider.length;
  }
  slideLeft();
});
// right arrow click
rightArrow.addEventListener("click", function() {
  if (current === slider.length - 1) {
    current = -1;
  }
  slideright();
});
startSlide();
/* main.css */
* {
  margin: 0;
  padding: 0;
}
/*---------BODY & BACKGROUND-----------*/
body {
  font-family: "Merienda";
  color: rgb(200, 150, 15);
  font-size: 0.8em;
  width: 100%;
  height: 100vh;
}
footer {
  height: 5vh;
  background-color: rgb(33, 33, 33);
  margin: 0 auto;
  display: flex;
  justify-content: center;
  flex-direction: column;
  text-align: center;
}
/*---------HEADING & MAIN SCREEN STUFF-----------*/
#home {
  background: linear-gradient(rgba(33, 33, 33, 0.9), rgba(33, 33, 33, 0.9)),
    url("/Images/Iphonexbackground.jpg");
  background-size: cover;
  width: 100%;
  height: calc(100vh - 20vh);
  padding-top: 15vh;
  display: flex;
  flex-flow: column;
  text-align: center;
}
.largeHeading {
  padding-bottom: 2vh;
}
.smallHeading {
  padding-bottom: 50vh;
}
.moe {
  color: white;
}
.social a {
  color: rgb(200, 150, 15);
}
.social a:hover {
  color: white;
  transition: all 0.5s ease-out;
}
/*------------------ROTATING MENU BUTTON----------------------*/
.menuButton {
  position: absolute;
  z-index: 3;
  right: 35px;
  top: 35px;
  cursor: pointer;
  transition: all 0.5s ease-out;
}
.buttonLine {
  width: 25px;
  height: 2px;
  background-color: white;
  margin: 0 0 5px 0;
  transition: all 0.5s ease-out;
}
.close > .buttonLine:nth-child(1) {
  transform: rotate(405deg) translate(5px, 5px);
}
.close > .buttonLine:nth-child(2) {
  opacity: 0;
}
.close > .buttonLine:nth-child(3) {
  transform: rotate(-405deg) translate(5px, -5px);
}
/*------------------------------FULL MENU----------------------------*/
.menu {
  width: 100vh;
}
/*Menu is closed by default*/
.menuWrapper {
  position: fixed;
  top: 0;
}
.show > .menuWrapper {
  width: 100%;
  margin: 0;
  padding: 0;
}
/*----------------------PORTRAIT MENU-----------------------*/
/*handles image*/
.myPortrait {
  transform: translate3d(-100%, 0, 0);
}
.show > .portrait {
  width: 150px;
  height: 150px;
  background-image: url("/Images/Portrait.jpg");
  background-size: cover;
  border: 3px solid rgb(200, 150, 15);
  border-radius: 50%;
  transform: translate3d(0, 0, 0);
  transition: all 0.5s ease-out;
}
.show > .myPortrait {
  visibility: visible;
  display: flex;
  flex-flow: column wrap;
  align-items: center;
  justify-content: center;
  float: none;
  width: 100%;
  height: 35vh;
  overflow: hidden;
  background-color: rgb(33, 33, 33);
  transform: translate3d(0, 0, 0);
  transition: all 0.5s ease-out;
}
/*----------------------LINK MENU-----------------------*/
.navLink {
  color: rgb(200, 150, 15);
  text-decoration: none;
}
.current > a {
  color: white;
  font-size: 1.5em;
}
.navItem a:hover {
  color: white;
  font-size: 1.5em;
}
.menuItems {
  visibility: hidden;
  transform: translate3d(100%, 0, 0);
}
.navItem {
  transform: translate3d(600px, 0, 0);
}
/*handles menue items*/
.show > .menuItems {
  visibility: visible;
  display: flex;
  flex-flow: column wrap;
  align-items: center;
  justify-content: center;
  float: none;
  width: 100%;
  height: 65vh;
  overflow: hidden;
  background-color: rgb(33, 33, 33);
  list-style: none;
  transform: translate3d(0, 0, 0);
  transition: all 0.5s ease-out;
}
/*Delays each individual link movments
coming from the right side*/
.show > .navItem:nth-child(1) {
  transform: translate3d(0, 0, 0);
  transition: all 0.5s ease-out;
  transition-delay: 0.1s;
  color: brown;
}
.show > .navItem:nth-child(2) {
  transform: translate3d(0, 0, 0);
  transition: all 0.5s ease-out;
  transition-delay: 0.2s;
  color: brown;
}
.show > .navItem:nth-child(3) {
  transform: translate3d(0, 0, 0);
  transition: all 0.5s ease-out;
  transition-delay: 0.3s;
  color: brown;
}
.show > .navItem:nth-child(4) {
  transform: translate3d(0, 0, 0);
  transition: all 0.5s ease-out;
  transition-delay: 0.4s;
  color: brown;
}
.show > .navItem:nth-child(5) {
  transform: translate3d(0, 0, 0);
  transition: all 0.5s ease-out;
  transition-delay: 0.5s;
  color: brown;
}
.show > .navItem:nth-child(6) {
  transform: translate3d(0, 0, 0);
  transition: all 0.5s ease-out;
  transition-delay: 0.6s;
  color: brown;
}
.show > .navItem:nth-child(7) {
  transform: translate3d(0, 0, 0);
  transition: all 0.5s ease-out;
  transition-delay: 0.7s;
  color: brown;
}
.show > .navItem:nth-child(8) {
  transform: translate3d(0, 0, 0);
  transition: all 0.5s ease-out;
  transition-delay: 0.8s;
  color: brown;
}

/*photography.css*/
#photography {
  background-color: rgb(66, 66, 66);
  background-size: cover;
  max-width: 100%;
  height: calc(100vh - 15vh);
  max-height: 100vh;
  padding-top: 10vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 0.7em;
}
.photoHeading {
  text-decoration: none;
  color: rgb(200, 150, 15);
  padding-bottom: 10px;
}
.photoWork {
  width: 90%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-gap: 10px;
  overflow: scroll;
}
.photoWork a {
  text-decoration: none;
}
.gallary {
  background-color: rgb(33, 33, 33);
  color: silver;
  text-align: center;
  cursor: pointer;
}
.gallary img {
  max-width: 100%;
}
.gallaryDiscription {
  padding: 2px;
}
.gallary:hover {
  opacity: 0.5;
  transform: scale(1.015);
}
.photoSocial {
  padding: 10px;
}
.photoContainer {
  width: 90%;
  height: 100vh;
  border: 2px solid white;
  overflow: scroll;
}
.slide {
  width: 100%;
  height: 100%;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}
.slide1 {
  background-image: url("/Images/Photography/Animal/FullAnimal-1.jpg");
}
.slide2 {
  background-image: url("/Images/Photography/Animal/FullAnimal-2.jpg");
}
.slide3 {
  background-image: url("/Images/Photography/Animal/FullAnimal-3.jpg");
}
.show > .photoContainer > .arrow {
  display: none;
}
.arrow {
  cursor: pointer;
  position: absolute;
  top: 50%;
  margin-top: 0px;
  width: 0;
  height: 0;
  border-style: solid;
}
.leftArrow {
  border-width: 30px 40px 30px 0;
  border-color: transparent rgba(255, 255, 255, 0.3) transparent transparent;
  left: 0;
  margin-left: 25px;
}
.rightArrow {
  border-width: 30px 0 40px 30px;
  border-color: transparent transparent transparent rgba(255, 255, 255, 0.3);
  right: 0;
  margin-right: 30px;
}
/*
.showImage img {
  width: 100%;
}
.gridImage img {
  width: 100%;
  height: calc(200px / 3);
}
.gridImage {
  display: grid;
  grid-gap: 2px;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(3, 33%);
}
*/

2 个答案:

答案 0 :(得分:0)

如果您不想使用JQuery,最好使用Only Bootstrap制作Image幻灯片。这是Bootstrap Carousel插件,请检查此链接https://www.w3schools.com/bootstrap/bootstrap_carousel.asp

答案 1 :(得分:0)

感谢您的帮助。我发现了问题: 我是在html中完成的:

<div class="slide2 slide"></div>

然后在CSS中执行此操作:

.slide2 {
  background-image: url("/Images/Photography/Animal/FullAnimal-2.jpg");
}

这使图像拉伸到适合div的位置,相反,我尝试直接在html中添加图像而不添加div,如下所示:

<img
          src="/Images/Photography/Animal/FullAnimal-1.jpg"
          alt=""
          class="slide1 slide"
        />

像这样从css拿走高度:100%:

.photoContainer {
  width: 90%;
  border: 2px solid white;
  overflow: scroll;
}
.slide {
  width: 100%;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

这使图像无论屏幕大小如何都占据其整个宽度和高度。我现在要做的就是垂直对齐图像,使其始终位于页面中央。 再次感谢您的所有帮助,这个谜底已解决!