世界各地的开发者们,你们好。
我想要在纯Javascript中得到一些帮助,制作一个简单的、不需要JQuery或其他库的拖动控制滑块,代码只有30行。
我已经搜索了数月,发现了许多脚本,但我不喜欢-Jquery,因为大多数脚本都需要4、5、6个javascript文件。我更喜欢更小、更基础的脚本。我希望自己可以根据需要进行调整,并且可以从较小的脚本中学到很多东西。
我所需要的仅仅是一个简单的滑块,用于:重新缩放图像、滚动页面、更改图像亮度(使用PHP)等等。
我是Javascript新手(2个月),这是我目前的水平。抱歉变量名不好。
<script type="text/javascript">
_item = null;
mouse_x = 0;
drag_x = 0;
function move_init() {
document.onmousemove = _move;
document.onmouseup = _stop;
}
function _stop(){
_item = null;
}
function _move(e){
mouse_x = document.all ? window.event.clientX : e.pageX;
if(_item != null){
_item.style.left = (mouse_x - drag_x) + "px";
}
}
function _move_item(drag)
{
_item = drag;
drag_x = mouse_x - _item.offsetLeft;
}
move_init();
drag.onmousedown=_move_item(); // Agh.. did'nt figure out how this works
</script>
<style type="text/css">
#drag{background:#797979;color:#fff;width:30px;height:15px;position:relative;}
#track{background:red; width:200px;}
</style>
<div id="track"><div id="drag" onmousedown="_move_item(this);" >x</div></div>
我感激你的帮助。
我写于2012年12月31日。祝新年快乐。请善待对方。
谢谢。