gpt4 book ai didi

javascript - 带有按钮和渐变的水平滚动区域

转载 作者:行者123 更新时间:2023-12-05 05:43:53 27 4
gpt4 key购买 nike

到目前为止,这是我的代码:

// Show and hide gradients

$(document).ready(function() {
$(".scroll-area").each(function(index) {
if ($(this)[0].scrollWidth <= $(this)[0].clientWidth) {
$(this).closest(".container").find(".left").css("display", "none");
$(this).closest(".container").find(".right").css("display", "none");
} else {
$(this).scroll(function() {
if ($(this)[0].scrollWidth > $(this)[0].clientWidth) {
if ($(this).scrollLeft() > 0) {
$(this).closest(".container").find(".left").css("display", "block");
}

if ($(this).scrollLeft() == 0) {
$(this).closest(".container").find(".left").css("display", "none");
}

var fullWidth = $(this)[0].scrollWidth - $(this)[0].offsetWidth - 1;

if ($(this).scrollLeft() >= fullWidth) {
$(this).closest(".container").find(".right").css("display", "none");
}

if ($(this).scrollLeft() < fullWidth) {
$(this).closest(".container").find(".right").css("display", "block");
}
}
});
}
});
});


// Scroll buttons

let interval;

$('.scroll-btn').on('mousedown', ({
target
}) => {
const type = $(target).attr('id');

interval = setInterval(() => {
var x = $('#a').scrollLeft();
$('#a').scrollLeft(type === 'left-arrow' ? x - 10 : x + 10);
}, 50);
});

$('.scroll-btn').on('mouseup', () => clearInterval(interval));
* {
margin: 0;
padding: 0;
font-family: sans-serif;
font-size: 16px;
}

.container {
width: 550px;
height: 80px;
background-color: grey;
position: relative;
margin-bottom: 20px;
}

.scroll-area {
white-space: nowrap;
overflow-x: auto;
height: 100%;
}

.left,
.right {
width: 50px;
height: 100%;
position: absolute;
pointer-events: none;
top: 0;
}

.left {
background: linear-gradient(90deg, orange 0%, rgba(0, 0, 0, 0) 100%);
left: 0;
display: none;
}

.right {
background: linear-gradient(-90deg, orange 0%, rgba(0, 0, 0, 0) 100%);
right: 0;
}

.arrow {
display: block;
position: absolute;
display: flex;
align-items: center;
justify-content: center;
height: 100%;
width: 15px;
cursor: pointer;
}

.left-arrow {
left: 0;
}

.right-arrow {
right: 0;
}

.left-arrow div,
.right-arrow div {
font-size: 40px;
}
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<div class="container">

<div id="x" class="left"></div>
<div class="right"></div>

<div class="arrow left-arrow">
<div class="scroll-btn" id="left-arrow">
<</div>
</div>
<div class="arrow right-arrow">
<div class="scroll-btn" id="right-arrow">></div>
</div>

<div id="a" class="scroll-area">
<div class="text">Scroll to right. The gradients and arrows should appear and disappear based on the scroll position. It should work with more than one container. Lorem ipsum.</div>
</div>
</div>

需求是:

  1. 箭头应该以与渐变相同的方式出现和消失。
  2. 如果没有足够的文本来形成可滚动区域,则不应有渐变和现在的箭头。
  3. 最后应该不止一个容器。

有人可以帮我做吗?我将非常感激!

最佳答案

您可以将箭头放在左/右渐变 div 中。这样,它们将以与渐变相同的方式显示/隐藏。

编辑

我稍微清理了一下代码,因为原来的答案有点乱。 (或者像 mstephen19 所说的那样‘怪异’:))。

// Show gradient and left/right arrows only if scrollable
$(".scroll-area").each((i, el) => {
$(el).parent().find(".right")[el.scrollWidth > el.clientWidth ? "show" : "hide"]();
});

// Show/hide gradient and arrows on scroll
$('.scroll-area').scroll((e) => {
const fullWidth = $(e.target)[0].scrollWidth - $(e.target)[0].offsetWidth - 1;
const left = $(e.target).scrollLeft()

$(e.target).parent().find(".left, .left-arrow")[left > 0 ? "show" : "hide"]();
$(e.target).parent().find(".right, .right-arrow")[left < fullWidth ? "show" : "hide"]();
});

// Scroll on left/right arrow mouse down
let intervalId;
$(".left-arrow, .right-arrow").on("mousedown", (e) => {
const scroll = $(e.target).closest(".container").find(".scroll-area");
intervalId = setInterval(() => {
const left = scroll.scrollLeft();
scroll.scrollLeft(e.target.classList.contains("left-arrow") ? left - 10 : left + 10);
}, 50);
}).on("mouseup mouseleave", () => {
clearInterval(intervalId);
});
* {
margin: 0;
padding: 0;
font-family: sans-serif;
font-size: 16px;
}

.container {
width: 550px;
height: 80px;
background-color: grey;
position: relative;
margin-bottom: 20px;
margin-left: 20px;
}

.scroll-area {
white-space: nowrap;
overflow-x: auto;
height: 100%;
}

.left,
.right {
width: 50px;
height: 100%;
position: absolute;
top: 0;
}

.left {
background: linear-gradient(90deg, orange 0%, rgba(0, 0, 0, 0) 100%);
left: 0;
display: none;
}

.right {
background: linear-gradient(-90deg, orange 0%, rgba(0, 0, 0, 0) 100%);
right: 0;
text-align: right;
}

.left-arrow,
.right-arrow {
margin: 0 10px;
position: absolute;
top: 50%;
-ms-transform: translateY(-50%);
transform: translateY(-50%);
cursor: pointer;
user-select: none;
font-size: 40px
}

.left-arrow {
display: none;
left: -25px;
}

.right-arrow {
right: -25px;
}
<html>

<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>

<body>
<div class="container">
<div class="left"></div>
<div class="right"></div>

<div class="left-arrow">&lt;</div>
<div class="right-arrow">&gt;</div>

<div class="scroll-area">
<div class="text">Scroll to right. The gradients and arrows should appear and disappear based on the scroll position. It should work with more than one container. Lorem ipsum.</div>
</div>
</div>

<div class="container">
<div class="left"><span class="left-arrow">&lt;</span></div>
<div class="right"><span class="right-arrow">&gt;</span></div>

<div class="scroll-area">
<div class="text">No scroll.</div>
</div>
</div>

</body>

</html>

关于javascript - 带有按钮和渐变的水平滚动区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71722861/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com