此滑块中加载的随机图像

时间:2017-08-06 10:12:01

标签: javascript html css3 random slider

我有来自codepen的这个滑块,我想稍后展示它的一些工作,例如第一张幻灯片将是'Photography',下一张可以是'插图'等等。

我想在每次刷新时随机选择3张图片(我现在有一张图片,但是还有另外两张用HTML注释)。

这是我的代码。

let dots = 4;
let sliderElem = document.querySelector(".slider");
let dotElems = sliderElem.querySelectorAll(".slider__dot");
let indicatorElem = sliderElem.querySelector(".slider__indicator");

Array.prototype.forEach.call(dotElems, dotElem => {
	dotElem.addEventListener("click", e => {
		let currentPos = parseInt(sliderElem.getAttribute("data-pos"));
		let newPos = parseInt(dotElem.getAttribute("data-pos"));

		let newDirection = newPos > currentPos ? "right" : "left";
		let currentDirection = newPos < currentPos ? "right" : "left";

		indicatorElem.classList.remove(`slider__indicator--${currentDirection}`);
		indicatorElem.classList.add(`slider__indicator--${newDirection}`);
		sliderElem.setAttribute("data-pos", newPos);
	});
});
.slider {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}
.slider__slides {
  position: relative;
  width: 400%;
  height: 100%;
  -webkit-transition: -webkit-transform 0.3s cubic-bezier(0.51, 0.92, 0.24, 1);
  transition: -webkit-transform 0.3s cubic-bezier(0.51, 0.92, 0.24, 1);
  transition: transform 0.3s cubic-bezier(0.51, 0.92, 0.24, 1);
  transition: transform 0.3s cubic-bezier(0.51, 0.92, 0.24, 1), -webkit-transform 0.3s cubic-bezier(0.51, 0.92, 0.24, 1);
  will-change: transform;
}
.slider[data-pos="0"] .slider__slides {
  -webkit-transform: translateX(0%);
          transform: translateX(0%);
}
.slider[data-pos="1"] .slider__slides {
  -webkit-transform: translateX(-25%);
          transform: translateX(-25%);
}
.slider[data-pos="2"] .slider__slides {
  -webkit-transform: translateX(-50%);
          transform: translateX(-50%);
}
.slider[data-pos="3"] .slider__slides {
  -webkit-transform: translateX(-75%);
          transform: translateX(-75%);
}
.slider__slide {
  float: left;
  width: 25%;
  height: 100%;
}
.slider__slide img {
  width:50%;
  display: block;
  margin: 0 auto;
}
.slider__dots {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  position: absolute;
  bottom: 1.5em;
  left: 50%;
  -webkit-transform: translateX(-50%);
          transform: translateX(-50%);
}
.slider__dot, .slider__indicator {
  display: block;
  margin: 0 0.5em;
  width: 1em;
  height: 1em;
  background: rgba(255, 255, 255, 0.5);
  border-radius: 100px;
}
.slider__indicator {
  position: absolute;
  background: white;
  width: auto;
}
.slider__indicator--left {
  -webkit-transition: left 0.3s cubic-bezier(0.51, 0.92, 0.24, 1.15), right 0.3s 0.1s cubic-bezier(0.51, 0.92, 0.24, 1.15);
  transition: left 0.3s cubic-bezier(0.51, 0.92, 0.24, 1.15), right 0.3s 0.1s cubic-bezier(0.51, 0.92, 0.24, 1.15);
}
.slider__indicator--right {
  -webkit-transition: left 0.3s 0.1s cubic-bezier(0.51, 0.92, 0.24, 1.15), right 0.3s cubic-bezier(0.51, 0.92, 0.24, 1.15);
  transition: left 0.3s 0.1s cubic-bezier(0.51, 0.92, 0.24, 1.15), right 0.3s cubic-bezier(0.51, 0.92, 0.24, 1.15);
}
.slider[data-pos="0"] .slider__indicator {
  left: 0em;
  right: 6em;
}
.slider[data-pos="1"] .slider__indicator {
  left: 2em;
  right: 4em;
}
.slider[data-pos="2"] .slider__indicator {
  left: 4em;
  right: 2em;
}
.slider[data-pos="3"] .slider__indicator {
  left: 6em;
  right: 0em;
}

.center {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  height: 100vh;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  background: #333;
}

.slider {
  max-width: 600px;
  max-height: 400px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.8);
}
.slider__slide:nth-child(1) {
  background: #309954;
}
.slider__slide:nth-child(2) {
  background: #FFBD3C;
}
.slider__slide:nth-child(3) {
  background: #F8593E;
}
.slider__slide:nth-child(4) {
  background: #4086FA;
}
<div class="center">
	
	<div class="slider" data-pos="0">
		<div class="slider__slides">
			<div class="slider__slide">
			  <img src="http://www.dogster.com/wp-content/uploads/2015/05/google-dog-search-2014-05.jpg">
        
        <!-- Second image that has a chance of showing upload load
        <img src="http://static4.businessinsider.com/image/569fdd9ac08a80bd448b7138/google-is-officially-a-dog-company.jpg">
      -->
      
      <!-- Third
        <img src="http://static4.businessinsider.com/image/569fdd9ac08a80bd448b7138/google-is-officially-a-dog-company.jpg">
      -->
        
        
        
        
			</div>
			<div class="slider__slide"></div>
			<div class="slider__slide"></div>
			<div class="slider__slide"></div>
		</div>
		<div class="slider__dots">
			<a href="#" class="slider__indicator"></a>
			<a href="#" class="slider__dot" data-pos="0"></a>
			<a href="#" class="slider__dot" data-pos="1"></a>
			<a href="#" class="slider__dot" data-pos="2"></a>
			<a href="#" class="slider__dot" data-pos="3"></a>
		</div>
	</div>
	
</div>

beleive 为了获得随机图片我需要以这种方式保存javascript,这是正确的吗?

 var imgs = [
        'http://www.dogster.com/wp-content/uploads/2015/05/google-dog-search-2014-05.jpg',
        'http://static4.businessinsider.com/image/569fdd9ac08a80bd448b7138/google-is-officially-a-dog-company.jpg',
        'http://static4.businessinsider.com/image/569fdd9ac08a80bd448b7138/google-is-officially-a-dog-company.jpg'
    ];

获得随机

var img_index = Math.floor( Math.random()*imgs.length );

但我如何知道将其整合到HTML中?

1 个答案:

答案 0 :(得分:1)

如果我正确理解您的问题,您希望在每次刷新后更改滑块的初始位置。

使用Math.random时,您的想法是正确的。

我已将您的代码修改为{J}代码中的sliderElem.setAttribute("data-pos", Math.floor(Math.random() * dots));。这将在加载Javascript文件时设置滑块的初始位置。

我还用你建议的图像填充了滑块组件。

修改

我已将代码更改为在第一张幻灯片中随机化图像。该幻灯片现在有一个id="first-slide,以便更容易找到。 如您所见,现在有一个函数getRandomImage()可以返回随机图像的URL。

您可以向imgs数组添加更多图像,该函数将继续从整个数组中选择一个随机图像。

我还将舍入功能从Math.floor更改为Math.round,以便为最后一张图像提供更多出现的机会。

&#13;
&#13;
let dots = 4;
let sliderElem = document.querySelector(".slider");
let dotElems = sliderElem.querySelectorAll(".slider__dot");
let indicatorElem = sliderElem.querySelector(".slider__indicator");

Array.prototype.forEach.call(dotElems, dotElem => {
  dotElem.addEventListener("click", e => {
    let currentPos = parseInt(sliderElem.getAttribute("data-pos"));
    let newPos = parseInt(dotElem.getAttribute("data-pos"));

    let newDirection = newPos > currentPos ? "right" : "left";
    let currentDirection = newPos < currentPos ? "right" : "left";

    indicatorElem.classList.remove(`slider__indicator--${currentDirection}`);
    indicatorElem.classList.add(`slider__indicator--${newDirection}`);
    sliderElem.setAttribute("data-pos", newPos);
  });
});

function getRandomImage() {
  let imgs = ['http://www.dogster.com/wp-content/uploads/2015/05/google-dog-search-2014-05.jpg', 'http://static4.businessinsider.com/image/569fdd9ac08a80bd448b7138/google-is-officially-a-dog-company.jpg'];
  let pos = Math.round(Math.random() * (imgs.length - 1));
  return imgs[pos];
}

document.getElementById('first-slide').setAttribute('src', getRandomImage());
/* Uncomment the line below to randomize slider initial position */
// sliderElem.setAttribute("data-pos", Math.floor(Math.random() * dots));
&#13;
.slider {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}
.slider__slides {
  position: relative;
  width: 400%;
  height: 100%;
  -webkit-transition: -webkit-transform 0.3s cubic-bezier(0.51, 0.92, 0.24, 1);
  transition: -webkit-transform 0.3s cubic-bezier(0.51, 0.92, 0.24, 1);
  transition: transform 0.3s cubic-bezier(0.51, 0.92, 0.24, 1);
  transition: transform 0.3s cubic-bezier(0.51, 0.92, 0.24, 1), -webkit-transform 0.3s cubic-bezier(0.51, 0.92, 0.24, 1);
  will-change: transform;
}
.slider[data-pos="0"] .slider__slides {
  -webkit-transform: translateX(0%);
          transform: translateX(0%);
}
.slider[data-pos="1"] .slider__slides {
  -webkit-transform: translateX(-25%);
          transform: translateX(-25%);
}
.slider[data-pos="2"] .slider__slides {
  -webkit-transform: translateX(-50%);
          transform: translateX(-50%);
}
.slider[data-pos="3"] .slider__slides {
  -webkit-transform: translateX(-75%);
          transform: translateX(-75%);
}
.slider__slide {
  float: left;
  width: 25%;
  height: 100%;
}
.slider__slide img {
  width:50%;
  display: block;
  margin: 0 auto;
}
.slider__dots {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  position: absolute;
  bottom: 1.5em;
  left: 50%;
  -webkit-transform: translateX(-50%);
          transform: translateX(-50%);
}
.slider__dot, .slider__indicator {
  display: block;
  margin: 0 0.5em;
  width: 1em;
  height: 1em;
  background: rgba(255, 255, 255, 0.5);
  border-radius: 100px;
}
.slider__indicator {
  position: absolute;
  background: white;
  width: auto;
}
.slider__indicator--left {
  -webkit-transition: left 0.3s cubic-bezier(0.51, 0.92, 0.24, 1.15), right 0.3s 0.1s cubic-bezier(0.51, 0.92, 0.24, 1.15);
  transition: left 0.3s cubic-bezier(0.51, 0.92, 0.24, 1.15), right 0.3s 0.1s cubic-bezier(0.51, 0.92, 0.24, 1.15);
}
.slider__indicator--right {
  -webkit-transition: left 0.3s 0.1s cubic-bezier(0.51, 0.92, 0.24, 1.15), right 0.3s cubic-bezier(0.51, 0.92, 0.24, 1.15);
  transition: left 0.3s 0.1s cubic-bezier(0.51, 0.92, 0.24, 1.15), right 0.3s cubic-bezier(0.51, 0.92, 0.24, 1.15);
}
.slider[data-pos="0"] .slider__indicator {
  left: 0em;
  right: 6em;
}
.slider[data-pos="1"] .slider__indicator {
  left: 2em;
  right: 4em;
}
.slider[data-pos="2"] .slider__indicator {
  left: 4em;
  right: 2em;
}
.slider[data-pos="3"] .slider__indicator {
  left: 6em;
  right: 0em;
}

.center {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  height: 100vh;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  background: #333;
}

.slider {
  max-width: 600px;
  max-height: 400px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.8);
}
.slider__slide:nth-child(1) {
  background: #309954;
}
.slider__slide:nth-child(2) {
  background: #FFBD3C;
}
.slider__slide:nth-child(3) {
  background: #F8593E;
}
.slider__slide:nth-child(4) {
  background: #4086FA;
}
&#13;
<div class="center">
  <div class="slider" data-pos="0">
    <div class="slider__slides">
      <div class="slider__slide">
        <img id="first-slide" src="http://www.dogster.com/wp-content/uploads/2015/05/google-dog-search-2014-05.jpg">
      </div>
      <div class="slider__slide">
        <img src="http://static4.businessinsider.com/image/569fdd9ac08a80bd448b7138/google-is-officially-a-dog-company.jpg">
      </div>
      <div class="slider__slide">
        <img src="http://www.dogster.com/wp-content/uploads/2015/05/google-dog-search-2014-05.jpg"></div>
      <div class="slider__slide">
        <img src="http://static4.businessinsider.com/image/569fdd9ac08a80bd448b7138/google-is-officially-a-dog-company.jpg">
      </div>
    </div>
    <div class="slider__dots">
      <a href="#" class="slider__indicator"></a>
      <a href="#" class="slider__dot" data-pos="0"></a>
      <a href="#" class="slider__dot" data-pos="1"></a>
      <a href="#" class="slider__dot" data-pos="2"></a>
      <a href="#" class="slider__dot" data-pos="3"></a>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;