HTML5商城开发二 通过位移实现拖动效果

1.效果

在该区域内,手按住拖动,该模块可上下滑动,至最顶或最底部,滑动出现空白区域将自动缩回

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUcAAAFXCAIAAAC+/RunAAAQyElEQVR4nO3d/0cuT//A8RWHRCKSiCMSOSQOOSQSR0TiiEQOkUQkEpHzn73vv+36/JAZOztf9nXN7O58rpnn46dr9+qanTvnee3svruvq2mUf//+NUM4PT09OTlJH2dpaenz8zN9nKZpzs7Ojo+P08f59u3bx8dH+jhN0/z+/fvXr1/p4ywvL7+/v6eP0zTN+fn50dFR+jgrKytvb2/p4zRNc3Fx8fPnz/RxVldXX19f08dpmuby8vLw8DB9nLW1tZeXl/Rxmqa5uro6ODhwP0fVvahagqolqDoVVUtQtQRVJ6FqCaqWoOpUVC1B1RJULUHVSahagqolMlTd+S1sbW09PDzozb29vZubG7358+fPi4sLvdmpuvNbuL293d3d1ZuPj4+bm5t68+3tbWVl5etxp+rOb2F7e/v+/l5v7u/vX19f682jo6Pz83O92am681u4u7vb2dnRm09PTxsbG3rz/f19eXn563Gn6vX19efnZ735/fv3v3//6s0fP378+fNHb/769ev37996s1P1nz9/fvz4oTf//v37/ft3vfn8/Ly+vq43Pz4+vn379vW4U/XGxsbT05Pe3NnZubu705sHBwdXV1d68/j4+OzsTG92qr6+vt7f39eb9/f329vbevPl5WVtbU1vfn5+Li0tfT3uVL25ufn4+Kg3d3d3b29v9ebh4eHl5aXePDk5OT091Zudqm9ubvb29vTmw8PD1taW3nx9fV1dXdWbNf971o+r/i1QdUPVSgH/nvXjqn8LVN1QtVLAv2f9uOrfAlU3VK0U8O9ZP676t0DVDVUrBfx7bv4pMwAL62eLLrzJPSsA8agaKA1VA6Uxql5Vcs8KQDyj6lcl96wAxGMFDpSGqoHSUDVQGqPqLSX3rADEM6p+UHLPCkC81BV49HKddT4wksGqbjx6XxgePEA+SaAqSVXbP9nZ4xtKeIjENwWgTkbVe4r9c87aw3vkSUfET9W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VAaagaKI1R9b6Se1YA4hlVXyu5ZwUgHitwoDRUDZSGqoHSGFUfKenjRr818J4CJDKqPlfSx21aHwzo1PvC3vED0ucPLK5RVuD2CE3sp5TJDxExCFCkDFXLk45Ll6pRueGrbq+97Z0zT3Xznr0b/1cIUDUqZ1R9pkQP57u+daZuP+sbM7DTPgpVo3JG1cdK+ri+C2ln8BKB8TsxUzUq17MCFybnOzP79oQ3bVQNyI14Xd3ZE84+8SjDDg4stIGrdp69JVXL197OEaga0Iyqr5SUEQOrZTvvwKvk+1mBA21G1QfKsMfIVTV5o06j/BXKjG+lB/IZ67q6vafzOP1c7TsbUzUwG/seuOSx3iO/WyaZ4VCLDmDhGFXfKcMewxlY9Mpc3ipVo05G1TtK7lkBiDfW3TIAuVA1UBqqBkpjVP2k5J4VgHhG1RtK7lkBiMcKHCgNVQOlaVf9f4JdCD4Ew0iPAAAAAElFTkSuQmCC" alt="" />

2.代码

<!DOCTYPE html>
<html>
<head>
<title>首页</title>
<style type="text/css">
.area-item {
height: 100px;
line-height:100px;
border-bottom: 1px solid #;
font-size: 50px;
}
</style>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src='js/jquery.mobile.custom.js'></script>
<script type="text/javascript">
$(function () {
$("#AreaList").height();
$("#AreaScroll").height(); //位置偏移量=真实高度-显示高度
var endPos = $("#AreaList").height() - $("#AreaScroll").height(); if (endPos > ) {
var startY = ;
var pos = ;//真实位置
$("#AreaScroll").on("vmousemove", function (event) {
$("#AreaList").css("transform", "translateY(" + (event.clientY - startY - pos) + "px)");
}); $("#AreaScroll").on("vmousedown", function (event) {
startY = event.clientY;
});
$("#AreaScroll").on("vmouseup", function (event) {
//判断正反方向并求位移
var posi = -pos + event.clientY - startY; if (posi > ) {
pos = ;
$("#AreaList").css("transform", "translateY(0px)");
}
if (posi < && posi >= -endPos) {
pos = -posi;
$("#AreaList").css("transform", "translateY(" + posi + "px)");
}
if (posi < -endPos) {
pos = endPos;
$("#AreaList").css("transform", "translateY(" + (-endPos) + "px)");
}
});
}
});
</script>
</head>
<body> <div id="AreaScroll" style="overflow: hidden;">
<div id="AreaList" style="transform: translateY(0px);">
<div class="area-item">0级台风</div>
<div class="area-item">一级台风</div>
<div class="area-item">一级台风</div>
<div class="area-item">一级台风</div>
<div class="area-item">一级台风</div>
<div class="area-item">11级台风</div>
<div class="area-item">一级台风</div>
<div class="area-item">一级台风</div>
<div class="area-item">一级台风</div>
<div class="area-item">一级台风</div>
<div class="area-item">12级台风</div>
<div class="area-item">一级台风</div>
<div class="area-item">一级台风</div>
<div class="area-item">一级台风</div>
<div class="area-item">一级台风</div>
<div class="area-item">13级台风</div>
<div class="area-item">一级台风</div>
<div class="area-item">一级台风</div>
<div class="area-item">一级台风</div>
<div class="area-item">一级台风</div>
<div class="area-item">2级台风</div>
</div>
</div> </body>
</html>
上一篇:JS实现页面进入、返回定位到具体位置


下一篇:linux命令总结