html左右滑动切换代码
html<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>左右滑动切换</title>
<style>
body {
margin: 0;
padding: 0;
overflow-x: hidden; /* 禁止横向滚动条出现 */
}
.container {
width: 100%;
overflow-x: hidden; /* 隐藏超出容器宽度的部分 */
white-space: nowrap; /* 内容在同一行上显示 */
}
.item {
display: inline-block;
width: 100%;
height: 100vh; /* 设置为视窗的高度 */
vertical-align: top;
}
</style>
</head>
<body>
<div class="container">
<div class="item" style="background-color: #ff6347;"></div> <!-- 第一个页面,橙红色 -->
<div class="item" style="background-color: #00bfff;"></div> <!-- 第二个页面,深天蓝色 -->
<div class="item" style="background-color: #3cb371;"></div> <!-- 第三个页面,适中的碧绿色 -->
</div>
<script>
let container = document.querySelector('.container');
let isDown = false;
let startX;
let scrollLeft;
container.addEventListener('mousedown', (e) => {
isDown = true;
startX = e.pageX - container.offsetLeft;
scrollLeft = container.scrollLeft;
});
container.addEventListener('mouseleave', () => {
isDown = false;
});
container.addEventListener('mouseup', () => {
isDown = false;
});
container.addEventListener('mousemove', (e) => {
if (!isDown) return;
e.preventDefault();
const x = e.pageX - container.offsetLeft;
const walk = (x - startX) * 3; // 控制滑动速度的系数
container.scrollLeft = scrollLeft - walk;
});
</script>
</body>
</html>
这段代码创建了一个容器,其中包含了三个页面,每个页面的宽度都是视窗的宽度,高度是视窗的高度。通过监听鼠标事件实现了左右滑动切换效果。
你可以复制粘贴代码到一个HTML文件中并在浏览器中打开查看效果。
html<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>左右滑动切换</title>
<style>
body {
margin: 0;
padding: 0;
overflow-x: hidden;
}
.container {
width: 100%;
overflow-x: hidden;
white-space: nowrap;
transition: transform 0.3s ease; /* 添加动画效果 */
}
.item {
display: inline-block;
width: 100vw; /* 改为视窗宽度 */
height: 100vh;
vertical-align: top;
}
</style>
</head>
<body>
<div class="container">
<div class="item" style="background-color: #ff6347;"></div>
<div class="item" style="background-color: #00bfff;"></div>
<div class="item" style="background-color: #3cb371;"></div>
</div>
<script>
let container = document.querySelector('.container');
let isDown = false;
let startX;
let scrollLeft;
container.addEventListener('mousedown', (e) => {
isDown = true;
startX = e.pageX - container.offsetLeft;
scrollLeft = container.scrollLeft;
});
container.addEventListener('touchstart', (e) => {
isDown = true;
startX = e.touches[0].pageX - container.offsetLeft;
scrollLeft = container.scrollLeft;
});
container.addEventListener('mouseleave', () => {
isDown = false;
});
container.addEventListener('mouseup', () => {
isDown = false;
});
container.addEventListener('touchend', () => {
isDown = false;
});
container.addEventListener('mousemove', (e) => {
if (!isDown) return;
e.preventDefault();
const x = e.pageX - container.offsetLeft;
const walk = (x - startX) * 3;
container.scrollLeft = scrollLeft - walk;
});
container.addEventListener('touchmove', (e) => {
if (!isDown) return;
e.preventDefault();
const x = e.touches[0].pageX - container.offsetLeft;
const walk = (x - startX) * 3;
container.scrollLeft = scrollLeft - walk;
});
</script>
</body>
</html>
这段代码中,我们添加了触摸事件以支持移动设备上的滑动操作,并为容器的滚动添加了过渡效果,使切换更加平滑。