基于所选滑块图像的自定义页面html

问题描述

很抱歉,如果我在这里错误地使用了一些术语,或者不知道正确描述这些术语的术语,但是...

简单的部分-我想创建一个“车轮”样式滑块,显示三张图片,主要的“选定”图片位于最前面,另外两张图片按比例缩小,在它们后面,但可以快速轻松地单击和查看。

硬部分-我希望将任何图片设置在滑轮的最前面的主要部分中,以显示有关该特定切换图片和该图片的信息(页面宽度)。然后,当您切换到其他幻灯片/图片时,只有关于该幻灯片/图片的信息会被放置在滑块/拨轮下方。

我想我的出发点是为切换/滑动图片获取一些代码。然后以某种方式为突出显示图片创建某种事件触发类型编码,并将其与某种html隐藏/显示编码结合起来。

我附上了一些不良的草图,以帮助我形象地描绘出我在说什么。

任何见解都是值得欢迎的,即使它是一些关键字可以帮助我缩小Google搜索范围并找到一些资源。 Slide & Page Layout Sketch

谢谢

解决方法

听起来像您需要一些CSS和JS才能使这项工作完成。

首先,您将需要HTML布局。我有整个旋转木马(.container)的包装纸。我有左右箭头以及用于图像的第二个包装器。

对于转盘下方的文本,我有第二个元素(.content),其中包含三个元素,每个元素与图像相关。仅在将.shown应用于元素时显示文本。

<div class="container">

  <div class="left">&lt;</div>
  
  <div>
    <div class="img img-left"></div>
    <div class="img img-center"></div>
    <div class="img img-right"></div>
  </div>

  <div class="right">&gt;</div>
  
</div>

<div class="content">
  <div class="text">
    Amazing Sunset
  </div>
  <div class="text shown">
    Fall Leaves
  </div>
  <div class="text">
    Misty Sunlight
  </div>
</div>

对于CSS,我选择制作.container position: relative,以便可以在孩子上使用position: absolute。我有3个图像班。 img-leftimg-rightimg-center。这些可以动画。箭头只是垂直居中

.container {
  position: relative;

  height: 85vh;
}

.img {
  position: absolute;
  z-index: 2;

  height: 75vh;
  width: 121vh;

  max-height: 300px;
  max-width: 511px;

  top: 0; bottom: 0;
  left: 0; right: 0;
  margin: auto;

  transition: transform 0.3s,z-index 0s linear 0.15s;
}

.img:nth-of-type(1) {
  background: url('https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__340.jpg') center/contain no-repeat;
}

.img:nth-of-type(2) {
  background: url('https://cdn.pixabay.com/photo/2015/12/01/20/28/road-1072823__340.jpg') center/contain no-repeat;
}

.img:nth-of-type(3) {
  background: url('https://cdn.pixabay.com/photo/2015/09/09/16/05/forest-931706__340.jpg') center/contain no-repeat;
}

.img-center {
  z-index: 5;
}

.img-right {
  transform: translateX(200px) scale(0.7);
}

.img-left {
  transform: translateX(-200px) scale(0.7);
}

.left,.right {
  position: absolute;
  z-index: 7;

  top: 50%;

  font-size: 48px;
  font-family: monospace;

  transform: translateY(-50%);
  user-select: none;
}

.left {
  left: 32px;
}

.right {
  right: 32px;
}

.content {
  height: 15vh;
}

.text {
  display: none;

  text-align: center;
  font-size: 32px;
}

.text.shown {
  display: block;
}

JavaScript使事情变得越来越有趣。我创建了一个名为nextImage()的函数,该函数接受一个表示切换方向的布尔值。首先,它获取当前居中的图像,然后基于该图像获取下一个和上一个元素同级。如果居中的图像恰好是第一个或最后一个元素,则nextpre将是undefined。接下来处理。完成此操作后,将根据方向重新分配CSS类。

function nextImage(forward) {
  let currentCentered = document.querySelector('.img-center'),next = currentCentered.nextElementSibling,pre = currentCentered.previousElementSibling;
  
  //pre and next may not be elements if currentCentered is the frist or last element.
  if (!next) { //Centered Element is the frist
    next = pre.previousElementSibling;
  } else if (!pre) { //Centered Element is the last
     pre = next.nextElementSibling;
  }
  
  if (forward) {    
    //Move the previously centered image to the right
    currentCentered.classList.remove('img-center');
    currentCentered.classList.add('img-right');

    //Move the previously left image to the center
    pre.classList.remove('img-left');
    pre.classList.add('img-center');

    //Move the previously right image to the left
    next.classList.remove('img-right');
    next.classList.add('img-left');
    
  } else {
  
    //Move the previously centered image to the left
    currentCentered.classList.remove('img-center');
    currentCentered.classList.add('img-left');

    //Move the previously left image to the right
    pre.classList.remove('img-left');
    pre.classList.add('img-right');

    //Move the previously right image to the center
    next.classList.remove('img-right');
    next.classList.add('img-center');
  }
  
  //Update the text
  let currentText = document.querySelector('.text.shown'),newText;
  
  if (forward) {
    //Get the previous element;
    newText = currentText.previousElementSibling;
    
    //If it doesn't exist get the last element
    if (!newText) {
      newText = currentText.nextElementSibling.nextElementSibling;
    }
  } else {
    //Get the next element;
    newText = currentText.nextElementSibling;
    
    //If it doesn't exist get the frist element
    if (!newText) {
      newText = currentText.previousElementSibling.previousElementSibling;
    }
  }
  
  //Apply class change
  currentText.classList.remove('shown');
  newText.classList.add('shown');
}

现在将onclick="nextImage(false)"添加到左箭头,并将onclick="nextImage(true)"添加到右箭头,即可进行导航。

自从您说您希望图片如此可点击以来,我添加了第二个功能,可让您滚动到给定的图像。它获取下一个和上一个元素,并确保它们是实际元素。然后它基于nextImage()next是居中图像来调用pre函数。

function switchImage(imgEle) {
  let next = imgEle.nextElementSibling,pre = imgEle.previousElementSibling;
    
  //Make sure they are actually elements  
  if (!next) {
    next = pre.previousElementSibling;
  }
  
  if (!pre) {
    pre = next.nextElementSibling;
  }
  
  if (next.classList.contains('img-center')) {
    nextImage(true);
  } else if (pre.classList.contains('img-center')) {
    nextImage(false);
  }
}

您现在要做的就是将onclick="switchImage(this)"添加到每个图像元素中。

将所有内容加在一起,您应该在下面获得类似此代码段的内容

function nextImage(forward) {
  let currentCentered = document.querySelector('.img-center'),newText;
  
  if (forward) {
    //Get the previous element;
    newText = currentText.previousElementSibling;
    
    //If it doesn't exist get the last element
    if (!newText) {
      newText = currentText.nextElementSibling.nextElementSibling;
    }
  } else {
    //Get the next element;
    newText = currentText.nextElementSibling;
    
    //If it doesn't exist get the frist element
    if (!newText) {
      newText = currentText.previousElementSibling.previousElementSibling;
    }
  }
  
  //Apply class change
  currentText.classList.remove('shown');
  newText.classList.add('shown');
}

function switchImage(imgEle) {
  let next = imgEle.nextElementSibling,pre = imgEle.previousElementSibling;
    
  //Make sure they are actually elements  
  if (!next) {
    next = pre.previousElementSibling;
  }
  
  if (!pre) {
    pre = next.nextElementSibling;
  }
  
  if (next.classList.contains('img-center')) {
    nextImage(true);
  } else if (pre.classList.contains('img-center')) {
    nextImage(false);
  }
}
.container {
  position: relative;
  
  height: 85vh;
}

.img {
  position: absolute;
  z-index: 2;

  height: 75vh;
  width: 121vh;

  max-height: 300px;
  max-width: 511px;

  top: 0; bottom: 0;
  left: 0; right: 0;
  margin: auto;
  
  transition: transform 0.3s,.right {
  position: absolute;
  z-index: 7;
  
  top: 50%;

  font-size: 48px;
  font-family: monospace;
  
  transform: translateY(-50%);
  user-select: none;
}

.left {
  left: 32px;
}

.right {
  right: 32px;
}

.content {
  height: 15vh;
}

.text {
  display: none;

  text-align: center;
  font-size: 32px;
}

.text.shown {
  display: block;
}
<div class="container">

  <div class="left" onclick="nextImage(false)">&lt;</div>
  
  <div>
    <div class="img img-left" onclick="switchImage(this)"></div>
    <div class="img img-center" onclick="switchImage(this)"></div>
    <div class="img img-right" onclick="switchImage(this)"></div>
  </div>

  <div class="right" onclick="nextImage(true)">&gt;</div>
  
</div>

<div class="content">
  <div class="text">
    Amazing Sunset
  </div>
  <div class="text shown">
    Fall Leaves
  </div>
  <div class="text">
    Misty Sunlight
  </div>
</div>