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

              
                .nav-container
	.controls.js-slidemenu-controls
		.prev
		.next
	.menu-container
		ul.menulist.js-slidemenu.js-scroll
			li
				a(href="#" data-no="1") Today's Deals Today's Deals Today's Deals Today's Deals
			li
				a(href="#" data-no="2") Registry Registry Registry Registry Registry Registry
			li
				a(href="#" data-no="3") Gift Cards Gift Cards Gift Cards
			li
				a(href="#" data-no="4") Customer Service Customer Service Customer Service
			li
				a(href="#" data-no="5") Registry Registry Registry



              
            
!

CSS

              
                body{
	margin:0;
}
.nav-container {
	position: fixed;
	margin: 0 auto;
	width: 100vw;
	/* ========================================================
	矢印
	=========================================================*/
	.controls {
		position: relative;
		// 左右ボタン(横幅、縦幅をJSの変数に指定)
		.prev,
		.next {
			position: absolute;
			width: 30px;
			height: 60px;
			background-color: rgb(124 124 124 / 80%);
			top: 0;
			z-index: 2;
			color: #fff;
			cursor: pointer;
			&:after {
				content: "";
				position: absolute;
				top: 50%;
				left: 50%;
				width: 8px;
				height: 8px;
				border-right: 2px solid #fff;
				border-bottom: 2px solid #fff;
				transform: translate(-50%, -50%) rotate(-45deg);
				box-sizing: border-box;
			}
			&.-disable {
				opacity: 0;
				pointer-events: none;
			}
		}
		.prev {
			left: 0;
			transform: scaleX(-1);
		}
		.next {
			right: 0;
		}
	}

	/*#########################################################

	MENU

	#########################################################*/
	.menu-container {
		background: #000;
		position: absolute;
		width: 100vw;

		.menulist {
			list-style: none;
			display: flex;
			justify-content: flex-start;
			align-items: center;
			margin: 0;
			height: 60px;

			&.js-slidemenu{
				overflow-x: auto;
				scrollbar-width: none;
				-ms-overflow-style: none;
				&::-webkit-scrollbar {
					display: none;
				}
			}

			> li {
				text-align: center;
				white-space: nowrap;
				&:first-child {
					> a {
						// margin-left: 20px;
					}
				}
				&:last-child {
					> a {
						margin-right: 20px;
						padding-right: 40px;
					}
				}
				> a {
					padding: 0 10px;
					color: #fff;
				}
			}
		}

	}
}
              
            
!

JS

              
                // 設定値
const btnWidth = 30;
const btnHeight = 60;
const controlName = ".js-slidemenu-controls";
const menuName = ".js-slidemenu";

// グローバル変数
let menulistElem;
let slideWidth = 0;

/* ========================================================
初期化
=========================================================*/
const initMenu = () => {
  slideWidth = 0;
  for (const element of menulistElem) {
    slideWidth += element.getBoundingClientRect().width;
  }
  document.querySelector(controlName + ' .prev')?.classList.add("-disable");
};

/* ========================================================
ナビの項目の番号取得
=========================================================*/
const getElementRect = (selector) => {
  const controlElem = document.querySelector(selector);
  const originalStyle = controlElem.style.cssText;

  controlElem.style.cssText += 'visibility:hidden;pointer-events:none;';
  const rect = controlElem.getBoundingClientRect();
  let elem = document.elementFromPoint(rect.left + btnWidth / 2, rect.top + btnHeight / 2);
  if (!elem?.matches("a")) {
    elem = elem?.querySelector("a");
  }
  controlElem.style.cssText = originalStyle;

  return Number(elem?.dataset.no ?? 1);
};

/* ========================================================
スクロール制御
=========================================================*/
const updateArrowState = (container) => {
  const prevBtn = document.querySelector(controlName + ' .prev');
  const nextBtn = document.querySelector(controlName + ' .next');
  const maxScroll = slideWidth - window.innerWidth;

  if (container.scrollLeft > btnWidth) {
    prevBtn?.classList.remove("-disable");
  } else {
    prevBtn?.classList.add("-disable");
  }
  if (container.scrollLeft < maxScroll) {
    nextBtn?.classList.remove("-disable");
  } else {
    nextBtn?.classList.add("-disable");
  }
};

/* ========================================================
矢印のクリック処理
=========================================================*/
const handleArrowClick = (isNext) => {
  const container = document.querySelector(menuName);
  const maxNo = container.childElementCount;
  let no = getElementRect(controlName + (isNext ? ' .next' : ' .prev'));

  no = isNext ? Math.min(no + 1, maxNo) : Math.max(no - 1, 1);
  const target = document.querySelector(`${menuName} a[data-no='${no}']`);
  const targetRect = target.getBoundingClientRect();
  const buttonRect = document.querySelector(controlName + (isNext ? ' .next' : ' .prev')).getBoundingClientRect();

  const offset = isNext
    ? container.scrollLeft + targetRect.right - buttonRect.left + window.pageXOffset
    : container.scrollLeft + targetRect.left + window.pageXOffset - 15;

  container.scrollLeft = offset;

  updateArrowState(container);
};

/* ========================================================
ドラッグスクロール対応
=========================================================*/
const mousedragscrollable = (selector) => {
  let target = null;
  const elms = document.querySelectorAll(selector);

  for (const elm of elms) {
    elm.addEventListener('mousedown', (evt) => {
      evt.preventDefault();
      target = elm;
      target.dataset.down = 'true';
      target.dataset.move = 'false';
      target.dataset.x = evt.clientX;
      target.dataset.y = evt.clientY;
      target.dataset.scrollleft = target.scrollLeft;
      target.dataset.scrolltop = target.scrollTop;
      evt.stopPropagation();
    });

    elm.addEventListener('click', (evt) => {
      if (elm.dataset.move === 'true') {
        evt.preventDefault();
        evt.stopPropagation();
      }
    });
  }

  document.addEventListener('mousemove', (evt) => {
    if (target?.dataset.down === 'true') {
      evt.preventDefault();
      const moveX = parseInt(target.dataset.x) - evt.clientX;
      const moveY = parseInt(target.dataset.y) - evt.clientY;
      if (moveX || moveY) {
        target.dataset.move = 'true';
        target.scrollLeft = parseInt(target.dataset.scrollleft) + moveX;
        target.scrollTop = parseInt(target.dataset.scrolltop) + moveY;
        evt.stopPropagation();
      }
    }
  });

  document.addEventListener('mouseup', (evt) => {
    if (target?.dataset.down === 'true') {
      target.dataset.down = 'false';
      evt.stopPropagation();
    }
  });
};

/* ========================================================
初期処理
=========================================================*/
window.addEventListener("DOMContentLoaded", () => {
  menulistElem = document.querySelectorAll(menuName + ' > li > a');
  initMenu();

  const container = document.querySelector(menuName);
  container.addEventListener("scroll", () => updateArrowState(container));

  document.querySelector(controlName + ' .prev')?.addEventListener("click", () => handleArrowClick(false));
  document.querySelector(controlName + ' .next')?.addEventListener("click", () => handleArrowClick(true));

  mousedragscrollable('.js-scroll');
});

window.addEventListener('resize', initMenu);

              
            
!
999px

Console