仿站网提醒您,本文共10403个字,系统预计阅读时间或需27分钟。
draggable="false",通过设置draggable,是可以设置html不允许拖拽效果,通过拖拽可以初步实现pc端横向滚动行为。 draggable的兼容性是最好HTML属性 css样式-webkit-user-drag: none;也可以实现类似效果,兼容性不太好,移动效果大部份都有效 user-select:属性可以设置是否允许用户选择页面中的图文内容 mousedown和mouseup:通过设置鼠标事件,实现鼠标按下后,坐标位置不一样,让容器调用scrollTo就可以实现滚动效果。 wheel:通过滚动事件,在容器内滚动滚轴可以横向滚动 getBoundingClientRect,记录每个图标的x位置,通过前后位置是否变化,如果不变化,鼠标单击的时候就可以触发单击事件。因为mousedown事件发生也会触发click事件由于容器隐藏横向滚动条后,移动端横向滚动效果不受影响,但是pc端是无法通过鼠标进行横向滚动,因此需要自己手动实现效果。
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>
最终实现的效果,如下图:
到此这篇关于css3手动实现pc端横向滚动的文章就介绍到这了,更多相关css3 pc端横向滚动内容请搜索仿站网www.ecmsw.cn以前的文章或继续浏览下面的相关文章,希望大家以后多多支持仿站网www.ecmsw.cn!
网站的功能是可以仿下来的,只是功能性的东西属于二次开发,不是网站拷贝那么简单。原网站的一些功能开发我们经常遇到的就是培训学员的证书查询...
问:我设置了301跳转,多久可以生效? 答:目前搜索引擎无法承诺301跳转的生效时间,因为站长感受到的生效时间会受多因素影响,比如Baiduspider再次抓取...
仿站网下面教大家如何确定站点目标?在创建网站时,决定站点的目标是第一步。设计者应清楚建立站点的目标,即确定它将提供什么样的服务,网页中...
仿站是指参照原先的网站的制作一个和原来的网站一样的,包括他的框架及内部的一些数据以及原先网站的功能都可以一 一仿出来。仿出来的网站也...
很多需要仿的客户似乎都有这样一个信念,认为仿站就是抄站,理所当然很简单很容易,所以不管是大站,小站难 站易站在他们眼中,都是一个级别的,在这里...
什么是内容复制?网站内容复制是指将一个网站的内容照搬到另一个网站,造成两个网站内容完全一样或者非常近似。为什么搜索引擎讨厌网站内容的...
仿站网专业提供帝国cms仿站及二次开发,质量保证!仿站价格请询问客服。请在咨询时将仿站目标网址发送给客服。
您给我们一个目标站,我们还您一个相同的网站
按照您的要求,全新设计+开发您独一无二的网页
模板使用遇到问题,我们协助您快速解决
给我新功能需求,帮您定制开发并免费集成到网站
对现有程序不满意,需要做程序调整和效果增加
提供效果图/psd文件,还您一个网页设计
下面列出来了仿站建站的流程