javascript - 试图将滚动条更改为箭头!没有 Bootstrap 请

标签 javascript html css

我已经尝试过这个我在网上找到的演示代码练习并创建了这个 slider ,使用滚动条更容易获得它,因为它们已经是为此制作的脚本。现在我正在尝试修改此脚本及其 css,以将滚动条更改为左右两侧的箭头。我已经尝试了一个月,现在正在看视频等等……什么都没有! Bootstrap 听起来更容易做到,但出于某种原因,它弄乱了我的网站。

$(document).ready(function() {
  $("#controller a").bind("click", function(event) {
    event.preventDefault();
    var target = $(".logo-container");
    $("#you").stop().animate({
      scrollLeft: $(target.next()).offset().left
    }, 1200);
  });
}); 
  
@media screen and (max-width:780px) {
	.logo-container {
		display: none;
	}
}

@media screen and (max-width:780px) {
	.logo-slider {
		display: none;
	}
}


/*background*/

.logo-slider {
	display: flex;
	flex-direction: row;
	overflow-x: scroll;
	-webkit-overflow-scrolling: touch;
	-ms-overflow-style: -ms-autohiding-scrollbar;
	width: 100%;
	padding-top: 2%;
	padding-bottom: 2%;
	position: relative;
}


/*text*/

.logo-container {
	background-color: #ffffff;
	width: calc(100%/5);
	box-shadow: 0 1px 2px rgba(0, 0, 0, .1);
	transition: all 0.3s ease;
	flex: 0 0 auto;
	margin-left: 2%;
	color: #000;
	border-style: solid;
	border-color: #000;
	border-width: 2px;
}


/*left margin*/

.first {
	margin-left: 1.5%;
}

#last {}


/*shadow on hover*/

.logo-container:hover {
	box-shadow: 0 15px 30px rgba(0, 0, 0, .1);
	transition: all 0.4s ease;
}

.logo img {
	max-width: 94%;
	border-style: solid;
	border-color: #000;
	margin-left: 2%;
	margin-top: 2%;
	border-width: 2px;
}


/*desc en bas*/

.logo-description {
	padding: 6%;
	text-align: center;
	text-transform: uppercase;
	font-weight: bold;
	font-size: 20px;
}

.scroller-controls {
	display: flex;
	flex-direction: row;
	justify-content: flex-start;
	background-color: #fff;
}


/*rendre images adaptable*/

.responsive {
	width: 100%;
	height: auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<section id="you" class="logo-slider">
   <div id="first" class="logo-container first">
      <div class="logo">
         <img src=";RS=300" alt="Texte alternatif" class="responsive" />
      </div>
      <div class="logo-description"><strong></strong></div>
      <p style="text-align: center;"> </p>
   </div>
   <div class="logo-container">
      <div class="logo">
         <img src=";RS=300" alt="Texte alternatif" class="responsive" />
      </div>
      <div class="logo-description"><strong></strong></div>
      <p style="text-align: center;"> </p>
   </div>
   <div class="logo-container">
      <div class="logo">
         <img src=";RS=300" alt="Texte alternatif" class="responsive" />
      </div>
      <div class="logo-description"><strong></strong></div>
      <p style="text-align: center;"> </p>
   </div>
   <div class="logo-container">
      <div class="logo">
         <img src=";RS=300" alt="Texte alternatif" class="responsive" />
      </div>
      <div class="logo-description"><strong>
         </strong>
      </div>
      <p style="text-align: center;"></p>
   </div>
   <div id="end" class="logo-container">
      <div class="logo">
         <img src=";;RS=300" alt="Texte alternatif" class="responsive" />
      </div>
      <div class="logo-description"><strong>
         </strong>
      </div>
      <p style="text-align: center;"> </p>
   </div>
</section>

最佳答案

jQuery 文档中的一些研究。经过一番思考,我想到了这个解决方案:

(($) => {
  
  let scrollValue = 0;  // Store the current position
  const list = $('#scroll-with-buttons');
  const maxHorizontalScroll = 1000;
  const scrollSpeed = 20;
  let direction = 'none';
  let scrollInterval = null;
  
  function startScrolling() {
    if (direction == 'L') {
      scrollValue -= scrollSpeed;

      if (scrollValue < 1) {
        scrollValue = 0;
      }

      list.scrollLeft(scrollValue);
    }
    else if (direction == 'R') {
      scrollValue += scrollSpeed;

      if (scrollValue > maxHorizontalScroll) {
        scrollValue = maxHorizontalScroll;
      }

      list.scrollLeft(++scrollValue);
    }
  }
  
  $('#left-scroll').on('mousedown', () => {
    direction = 'L';
    scrollInterval = setInterval(startScrolling, 10);
  });
  
  $('#right-scroll').on('mousedown', () => {
    direction = 'R';
    scrollInterval = setInterval(startScrolling, 10);
  });
  
  $(document).mouseup(() => {
    direction = '';
    clearInterval(scrollInterval);
  });
  
})(jQuery);
.elements {
  list-style-type: none;
  overflow: auto;
  overflow-x: hidden;  /*hide default scrollbar*/
  white-space: nowrap;  /*makes list scrollable*/
  margin: 0;
  padding: 0;
}

.element {
  display: inline-block;
  
  padding: 5px;
  height: 50px;
  width: 100px;
  background: rgb(37, 37, 37);
  color: rgb(240, 240, 240);
}

#left-scroll,
#right-scroll {
  font-size: 25px;
}

#left-scroll {
  float: left;
}

#right-scroll {
  float: right;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<ul id="scroll-with-buttons" class="elements">
  <li class="element">Element 1</li>
  <li class="element">Element 2</li>
  <li class="element">Element 3</li>
  <li class="element">Element 4</li>
  <li class="element">Element 5</li>
  <li class="element">Element 6</li>
  <li class="element">Element 7</li>
  <li class="element">Element 8</li>
  <li class="element">Element 9</li>
  <li class="element">Element 10</li>
</ul>

<button id="left-scroll"><i class="fas fa-arrow-circle-left"></i></button>
<button id="right-scroll"><i class="fas fa-arrow-circle-right"></i></button>

关于javascript - 试图将滚动条更改为箭头!没有 Bootstrap 请,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59326725/

相关文章:

javascript - 在 IE 和 Edge 中,在子窗口上调用 acquireNode 和 importNode 失败

css - #map-canvas 的 child 可以在 map 前面吗?

html - CSS 一个列表,所以它的大小相同

html - 使用 Chrome DevTools 插入内联 CSS 规则是一种好的编码习惯吗?

jquery - 使用 "list-item"使文本标题在单词和样式中分开,并使用 "div"父级自动拉伸(stretch)

javascript - Safari 浏览器在旋转变换后渲染 SVG 两次

javascript - 长代码变成函数?

javascript - 未知提供者 : uniqueFilterProvider using unique filter in Angular. js

新闻发布链接的 CSS 间距

javascript动态添加类而不使用内联函数