使用js实现推拉式菜单


<style>#Cai1 {
BACKGROUND-COLOR: #336699;
BORDER-BOTTOM: white 2px outset;
BORDER-LEFT: white 2px outset;
BORDER-RIGHT: white 2px outset;
BORDER-TOP: white 2px outset; LEFT: 0px;
POSITION: absolute; TOP: 70px; VISIBILITY: hidden;
WIDTH: 200px; layer-background-color: lightblue
}
</style>
<script language="javascript">
<!--
function menuIn() {                         // 菜单隐藏
clearTimeout(out_ID)                        // 清除定时器
if( menu.pixelLeft > menuW*-1+20 ) {
 menu.pixelLeft -= 2                 // 菜单位置递减
 in_ID = setTimeout("menuIn()", 1)   // 启动定时器
}
}

function menuOut() {                        // 菜单出现
clearTimeout(in_ID)                         // 清除定时器
if( menu.pixelLeft < 0) {
 menu.pixelLeft += 2                 // 菜单位置递增
 out_ID = setTimeout("menuOut()", 1) // 启动定时器
}
}


function cOver() {
clearTimeout(F_out)
F_over = setTimeout("menuOut()", 10)
}


function cOut() {
clearTimeout(F_over)
F_out = setTimeout("menuIn()", 10)
}


function init() {
menu = Cai1.style
menuW = Cai1.offsetWidth
Cai1.style.pixelLeft = menuW*-1+20             // 设置菜单初始位置
Cai1.onmouseover = cOver                       // 鼠标划过时调用cOver函数
Cai1.onmouseout = cOut                         // 鼠标离开时调用cOut函数
Cai1.style.visibility = "visible"              // 显示图层
}
F_over=F_out=in_ID=out_ID=null
//-->
</script>
</head>
<body οnlοad="init()">
<div id="Cai1">
<table border="0">
<tr>
 <td width=40>&nbsp;</td><td align="center"><font style="COLOR: #ffffff">菜单</font></td>
</tr>
<tr>
 <td width=40>&nbsp;</td><td><a href="http://www.sina.com.cn"><font color="#cccccc" size=2>新浪网</font></a></td>
</tr>
</tr>
 <td width=40>&nbsp;</td><td><a href="http://www.sohu.com"><font color="#cccccc" size=2>搜狐</font></a></td>
</tr>
<tr>
 <td width=40>&nbsp;</td><td><a href="http://www.163.com"><font color="#cccccc" size=2>网易</font></a></td>
</tr>
</table>
</div>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现推拉滚动效果可以通过以下步骤: 1. 在 HTML 中创建一个包含内容的容器,设置其高度和宽度,并将 `overflow` 属性设置为 `hidden`,以隐藏超出容器大小的内容。 ```html <div class="container"> <p>第一页的内容</p> <p>第二页的内容</p> <p>第三页的内容</p> </div> ``` 2. 使用 JavaScript 获取容器和内容的高度,并计算每个页面的高度。 ```javascript const container = document.querySelector('.container'); const content = container.querySelector('p'); const contentHeight = content.offsetHeight; const containerHeight = container.offsetHeight; const pageCount = Math.ceil(contentHeight / containerHeight); const pageHeight = contentHeight / pageCount; ``` 3. 创建一个变量 `currentPage` 来跟踪当前页面的索引,初始值为 0。在容器中创建一个滑动条,用于控制页面的滚动。 ```javascript let currentPage = 0; const slider = document.createElement('div'); slider.className = 'slider'; container.appendChild(slider); ``` 4. 为滑动条添加事件监听器,在滑动条被拖动时更新页面的位置。 ```javascript slider.addEventListener('mousedown', startDragging); function startDragging(e) { const startY = e.clientY; const startScrollTop = container.scrollTop; document.addEventListener('mousemove', drag); document.addEventListener('mouseup', stopDragging); function drag(e) { const deltaY = e.clientY - startY; container.scrollTop = startScrollTop + deltaY; } function stopDragging() { document.removeEventListener('mousemove', drag); document.removeEventListener('mouseup', stopDragging); } } ``` 5. 在滑动容器时,根据当前滚动位置计算出当前页数,并将滑动条的位置设置为当前页数的百分比。 ```javascript container.addEventListener('scroll', () => { const scrollTop = container.scrollTop; const currentPage = Math.floor(scrollTop / pageHeight); const sliderPosition = (currentPage + 0.5) / pageCount * 100; slider.style.top = `${sliderPosition}%`; }); ``` 6. 当滑动条被拖动时,根据滑动条的位置计算出当前页数,并将容器滚动到当前页的位置。 ```javascript function drag(e) { const sliderY = e.clientY - container.getBoundingClientRect().top; const sliderPosition = Math.max(0, Math.min(sliderY / containerHeight, 1)); const currentPage = Math.floor(sliderPosition * pageCount); const scrollTop = currentPage * pageHeight; container.scrollTop = scrollTop; } ``` 完整代码如下: ```html <style> .container { height: 200px; width: 300px; overflow: hidden; position: relative; } .slider { position: absolute; top: 50%; left: calc(100% - 10px); transform: translate(-50%, -50%); width: 10px; height: 50px; background-color: gray; border-radius: 5px; cursor: pointer; transition: top 0.2s ease-in-out; } </style> <div class="container"> <p>第一页的内容</p> <p>第二页的内容</p> <p>第三页的内容</p> <div class="slider"></div> </div> <script> const container = document.querySelector('.container'); const content = container.querySelector('p'); const contentHeight = content.offsetHeight; const containerHeight = container.offsetHeight; const pageCount = Math.ceil(contentHeight / containerHeight); const pageHeight = contentHeight / pageCount; let currentPage = 0; const slider = document.createElement('div'); slider.className = 'slider'; container.appendChild(slider); slider.addEventListener('mousedown', startDragging); function startDragging(e) { const startY = e.clientY; const startScrollTop = container.scrollTop; document.addEventListener('mousemove', drag); document.addEventListener('mouseup', stopDragging); function drag(e) { const deltaY = e.clientY - startY; container.scrollTop = startScrollTop + deltaY; } function stopDragging() { document.removeEventListener('mousemove', drag); document.removeEventListener('mouseup', stopDragging); } } container.addEventListener('scroll', () => { const scrollTop = container.scrollTop; const currentPage = Math.floor(scrollTop / pageHeight); const sliderPosition = (currentPage + 0.5) / pageCount * 100; slider.style.top = `${sliderPosition}%`; }); function drag(e) { const sliderY = e.clientY - container.getBoundingClientRect().top; const sliderPosition = Math.max(0, Math.min(sliderY / containerHeight, 1)); const currentPage = Math.floor(sliderPosition * pageCount); const scrollTop = currentPage * pageHeight; container.scrollTop = scrollTop; } </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值