Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1><strong>Constant velocity scroll slider independent of number</strong><br> Vanilla JS (Web Animations API)</h1>

<div class="wrapper">
  <div class="js-scroll-slider">
    <div class="js-scroll-slider__item"><img src="https://picsum.photos/id/275/400/400" alt="" width="400" height="400"></div>
    <div class="js-scroll-slider__item"><img src="https://picsum.photos/id/200/400/400" alt="" width="400" height="400"></div>
    <div class="js-scroll-slider__item"><img src="https://picsum.photos/id/1062/400/400" alt="" width="400" height="400"></div>
    <div class="js-scroll-slider__item"><img src="https://picsum.photos/id/1003/400/400" alt="" width="400" height="400"></div>
    <div class="js-scroll-slider__item"><img src="https://picsum.photos/id/1001/400/400" alt="" width="400" height="400"></div>
  </div>

  <div class="js-scroll-slider is-reverse">
    <div class="js-scroll-slider__item"><img src="https://picsum.photos/id/270/400/400" alt="" width="400" height="400"></div>
    <div class="js-scroll-slider__item"><img src="https://picsum.photos/id/10/400/400" alt="" width="400" height="400"></div>
    <div class="js-scroll-slider__item"><img src="https://picsum.photos/id/103/400/400" alt="" width="400" height="400"></div>
  </div>
</div>
              
            
!

CSS

              
                .wrapper {
  overflow: hidden;
}
.js-scroll-slider {
  display: flex;
  margin-bottom: 20px;
}
.js-scroll-slider__item {
  flex-shrink: 0;
  width: 400px;
  margin-right: 20px;
}


/* テストページ設定 */
body {
  margin: 0;
  padding: 50px 0;
}
h1 {
  text-align: center;
  margin-bottom: 50px;
  font-size: 30px;
}
strong {
  font-size: 150%;
}
img {
  max-width: 100%;
  vertical-align: top;
}
              
            
!

JS

              
                //要素数が変わっても一定の速度を保つ等速スクロールスライダー
const scrollSlider = document.querySelectorAll('.js-scroll-slider');
if(scrollSlider.length > 0) {
	//スクロールスピード設定
	const speed = 0.05;

	scrollSlider.forEach((slider) => {
		//スライド要素を取得
		const children = slider.children;
		const childLength = children.length;
		//スライド要素一式を文字列で取得
		let baseChildren = '';
		for (let i = 0; i < children.length; i++) {
			baseChildren += children[i].outerHTML;
		}

		//スライド要素の横幅を取得
		const firstChild = slider.firstElementChild;
		const styles = getComputedStyle(firstChild);

		//スライダー定義関数
		let winWidth = window.innerWidth;
		let sliderWidth;
		let countWidth = 0;
		let addCount = 1;
		const initializeSlider = (countWidth, addCount) => {
			//画面外まで表示を確保するため要素を複製
			let checkWidth = winWidth * 2;
			let width = parseFloat(styles.width);
			let marginRight = parseFloat(styles.marginRight);
			sliderWidth = (width + marginRight) * childLength;
			while(countWidth < checkWidth) {
				slider.insertAdjacentHTML('beforeend',baseChildren);
				++addCount;
				countWidth = sliderWidth * addCount;
			}
		}
		initializeSlider(countWidth, addCount);

		//反転処理
		let unit = '-';
		const isReverse = slider.classList.contains('is-reverse');
		if(isReverse) {
			unit = '';
			slider.style.marginLeft = '-' + sliderWidth + 'px';
		}

		//アニメーションセット
		const keyframes = {
			transform: 'translateX('+ unit + sliderWidth + 'px)'
		};
		const timing = {
			fill: 'backwards',
			duration: sliderWidth / speed,
			easing: 'linear',
			iterations: Infinity
		};
		let slideAnime = slider.animate(keyframes,timing);

		//レスポンシブ対応
		let timeoutId;
		let lastWinWidth = window.innerWidth;
		window.addEventListener('resize', () => {
			clearTimeout(timeoutId);

			//リサイズを停止した時のみ処理
			timeoutId = setTimeout(() => {
				// 現在と前回の横幅が違う場合だけ実行
				if (lastWinWidth !== window.innerWidth) {
					lastWinWidth = window.innerWidth;
					winWidth = lastWinWidth;

					//スライダーが途切れないかチェック
					initializeSlider(countWidth, addCount);

					//設定をリセット
					if(isReverse) {
						slider.style.marginLeft = '-' + sliderWidth + 'px';
					}
					slideAnime.effect.updateTiming({iterations:1});
					slideAnime.finish();

					//スライダー再定義
					keyframes.transform = 'translateX('+ unit + sliderWidth + 'px)';
					timing.duration = sliderWidth / speed;
					slideAnime = slider.animate(keyframes,timing);
				}
			}, 500);
		});
	});
}
              
            
!
999px

Console