此滑块中加载的随机图像

时间:2022-11-04 09:44:12

I've got this slider from codepen, and I wanted to showcase some work on it later, for example the first slide will be 'Photography' next one can be 'Illustrations' and so on.

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

I want to have 3 images that are random chosen on each refresh (I have now one that shows but there are 2 other written in comment in HTML).

我希望在每次刷新时随机选择3个图像(我现在有一个显示但在HTML中有另外两个用注释写的)。

Here is my code.

这是我的代码。

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>

I beleive in order to have the random images I need to have a var saved in javascript this way, is that correct?

我相信,为了获得随机图像,我需要以这种方式保存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'
    ];

To get a random one:

获得一个随机的:

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

But how do I know integrate this in HTML?

但我怎么知道在HTML中集成这个?

1 个解决方案

#1


1  

If I understand your question correctly, you want to change the initial position of the slider after every refresh.

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

Your idea was in the right direction with the Math.random use.

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

I have modified your code a little bit adding sliderElem.setAttribute("data-pos", Math.floor(Math.random() * dots)); to your Javascript code. This will set the initial position of the slider when the Javascript file loads.

我稍微修改了你的代码,添加了sliderElem.setAttribute(“data-pos”,Math.floor(Math.random()* dots));到您的Javascript代码。这将在加载Javascript文件时设置滑块的初始位置。

I have also filled the slider component with the images you suggested.

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

Edit

I have changed the code to randomize the image at the first slide. That slide now have an id="first-slide to make it more easy to find. As you can see, now there is a function getRandomImage() that returns the URL of a random image.

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

You can add more images to the imgs array and the function will continue selecting a random one from the complete array.

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

I have also changed the rounding function from Math.floor to Math.round to give the last image more chances of appearing.

我还将舍入函数从Math.floor更改为Math.round,以使最后一个图像有更多出现的机会。

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));
.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 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>

#1


1  

If I understand your question correctly, you want to change the initial position of the slider after every refresh.

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

Your idea was in the right direction with the Math.random use.

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

I have modified your code a little bit adding sliderElem.setAttribute("data-pos", Math.floor(Math.random() * dots)); to your Javascript code. This will set the initial position of the slider when the Javascript file loads.

我稍微修改了你的代码,添加了sliderElem.setAttribute(“data-pos”,Math.floor(Math.random()* dots));到您的Javascript代码。这将在加载Javascript文件时设置滑块的初始位置。

I have also filled the slider component with the images you suggested.

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

Edit

I have changed the code to randomize the image at the first slide. That slide now have an id="first-slide to make it more easy to find. As you can see, now there is a function getRandomImage() that returns the URL of a random image.

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

You can add more images to the imgs array and the function will continue selecting a random one from the complete array.

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

I have also changed the rounding function from Math.floor to Math.round to give the last image more chances of appearing.

我还将舍入函数从Math.floor更改为Math.round,以使最后一个图像有更多出现的机会。

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));
.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 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>