CSS怎样实现容器在PC端的横向滚动效果
Admin 2022-09-21 群英技术资讯 1375 次浏览
本篇内容介绍了“CSS怎样实现容器在PC端的横向滚动效果”的有关知识,在实际项目的操作过程或是学习过程中,不少人都会遇到这样的问题,接下来就让小编带大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!由于容器隐藏横向滚动条后,移动端横向滚动效果不受影响,但是pc端是无法通过鼠标进行横向滚动,因此需要自己手动实现效果。
draggable="false",通过设置draggable,是可以设置html不允许拖拽效果,通过拖拽可以初步实现pc端横向滚动行为。
-webkit-user-drag: none;也可以实现类似效果,兼容性不太好,移动效果大部份都有效user-select:属性可以设置是否允许用户选择页面中的图文内容mousedown和mouseup:通过设置鼠标事件,实现鼠标按下后,坐标位置不一样,让容器调用scrollTo就可以实现滚动效果。wheel:通过滚动事件,在容器内滚动滚轴可以横向滚动getBoundingClientRect,记录每个图标的x位置,通过前后位置是否变化,如果不变化,鼠标单击的时候就可以触发单击事件。因为mousedown事件发生也会触发click事件class Scroller {
init() {
this.setDragWheelEvent(".gameShow");
this.setDragScrollEvent(".gameShow");
this.initClick();
}
throttle(fn, wait) {
let inThrottle, lastFn, lastTime;
return function () {
const context = this, args = arguments;
if (!inThrottle) {
fn.apply(context, args);
lastTime = Date.now();
inThrottle = true;
} else {
clearTimeout(lastFn);
lastFn = setTimeout(function () {
if (Date.now() - lastTime >= wait) {
fn.apply(context, args);
lastTime = Date.now();
}
}, Math.max(wait - (Date.now() - lastTime), 0));
}
};
}
setDragWheelEvent(selector) {
const gameShowEle = document.querySelector(selector);
gameShowEle.addEventListener("wheel", (event) => {
event.preventDefault();
gameShowEle.scrollLeft += event.deltaY;
});
}
setDragScrollEvent(selector) {
const gameShowEle = document.querySelector(selector);
let left = 0;
let oldLeft = 0;
const move = this.throttle((event) => {
let x = left + (oldLeft - event.clientX)
if (x < 0) x = 0;
gameShowEle.scrollTo(x, 0)
}, 100)
gameShowEle.addEventListener('mousedown', function (event) {
gameShowEle.style.cursor = 'grabbing';
gameShowEle.style.userSelect = 'none';
oldLeft = event.clientX;
left = gameShowEle.scrollLeft;
document.addEventListener('mousemove', move)
});
document.addEventListener('mouseup', function () {
gameShowEle.style.cursor = 'pointer';
gameShowEle.style.removeProperty('user-select');
document.removeEventListener('mousemove', move)
})
}
isMobile() {
return window.navigator.userAgent.match(
/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|Symbian|Windows Phone)/i
);
}
initClick() {
const imgSpaceEles = document.querySelectorAll(".imgSpace");
if (imgSpaceEles) {
const xAarry = [];
Array.from(imgSpaceEles).forEach((imgSpaceEle, index) => {
const href = imgSpaceEle.getAttribute("url");
let { x } = imgSpaceEle.getBoundingClientRect();
xAarry.push(x);
imgSpaceEle.addEventListener("click", () => {
let { x: newx } = imgSpaceEle.getBoundingClientRect();
if (xAarry[index] == newx || this.isMobile()) {
alert(href)
}
xAarry.forEach((m, i) => {
const ele = imgSpaceEles[i];
const site = ele.getBoundingClientRect();
xAarry[i] = site.x
})
})
})
}
}
}
window.onload = () => {
const scroller = new Scroller()
scroller.init();
}
<style>
.gameMenu {
overflow: hidden;
margin: 0 auto;
height: 100%;
}
.gameMenu>div {
display: flex;
flex-direction: column;
justify-content: center;
align-content: center;
box-sizing: border-box;
margin: auto;
padding: 10px 10px 0 10px;
border-top-left-radius: 10px;
border-top-right-radius: 10px;
width: 320px;
height: 100%;
background: #fff;
}
.games {
border-radius: 10px;
width: 100%;
height: 90px;
box-shadow: rgb(0 0 0 / 16%) 0 0 10px 0;
}
.navigationStyle {
display: flex;
overflow: hidden;
position: relative;
justify-content: center;
align-items: center;
padding: 0 1px;
width: 100%;
height: 100%;
}
.gameShow {
display: flex;
overflow-x: scroll;
align-items: center;
width: inherit;
height: 90px;
cursor: pointer;
}
.gameShow::-webkit-scrollbar {
display: none;
}
.imgSpace {
margin: 5px;
width: 60px;
height: 60px;
}
</style>
<div class="gameMenu" style="width: 320px">
<div>
<div class="games">
<div id="navigationStyle" class="navigationStyle">
<div class="gameShow" draggable="false" style="cursor: pointer;">
<div class="imgSpace" url="/game/crazy-ball/play" title="crazy-ball">
<div style="position: relative">
<div
style="width: 60px; height: 60px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.16) 0px 9px 5px 0px; background-image: url("https://res.minigame.vip/gc-assets/crazy-ball/crazy-ball_icon.webp"); background-position: center center; background-repeat: no-repeat; background-size: contain;">
</div>
</div>
</div>
<div class="imgSpace" url="/game/mutant-dino/play" title="mutant-dino">
<div style="position: relative">
<div
style="width: 60px; height: 60px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.16) 0px 9px 5px 0px; background-image: url("https://res.minigame.vip/gc-assets/mutant-dino/mutant-dino_icon.webp"); background-position: center center; background-repeat: no-repeat; background-size: contain;">
</div>
</div>
</div>
<div class="imgSpace" url="/game/plants-beatzombies/play" title="plants-beatzombies">
<div style="position: relative">
<div
style="width: 60px; height: 60px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.16) 0px 9px 5px 0px; background-image: url("https://res.minigame.vip/gc-assets/plants-beatzombies/plants-beatzombies_icon.webp"); background-position: center center; background-repeat: no-repeat; background-size: contain;">
</div>
</div>
</div>
<div class="imgSpace" url="/game/queen-hulahoop/play" title="queen-hulahoop">
<div style="position: relative">
<div
style="width: 60px; height: 60px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.16) 0px 9px 5px 0px; background-image: url("https://res.minigame.vip/gc-assets/queen-hulahoop/queen-hulahoop_icon.webp"); background-position: center center; background-repeat: no-repeat; background-size: contain;">
</div>
</div>
</div>
<div class="imgSpace" url="/game/popstone2/play" title="popstone2">
<div style="position: relative">
<div
style="width: 60px; height: 60px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.16) 0px 9px 5px 0px; background-image: url("https://res.minigame.vip/gc-assets/popstone2/popstone2_icon.webp"); background-position: center center; background-repeat: no-repeat; background-size: contain;">
</div>
</div>
</div>
<div class="imgSpace" url="/game/ninja-sword/play" title="ninja-sword">
<div style="position: relative"></div>
<div
style="width: 60px; height: 60px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.16) 0px 9px 5px 0px; background-image: url("https://res.minigame.vip/gc-assets/ninja-sword/ninja-sword_icon.webp"); background-position: center center; background-repeat: no-repeat; background-size: contain;">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了CSS实现三栏布局中间一栏自适应且随文字大小变化宽度的示例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
这篇文章主要介绍了前端Html5如何实现分享截图的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
一些朋友对innerHTML的使用比较好奇,但又找不到比较合适的方法,接下来的时间不妨看看爱站技术频道小编所整理的资料,或多或少能在工作中帮助到大家。
这篇文章主要给大家介绍了关于如何利用css隐藏input光标的相关资料,这是最近工作中遇到的一个需求,虽然看似不合理,但是有需要就要有解决的办法,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
这篇文章主要介绍了html中使用vue-router的示例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008