请参阅小提琴:http://jsfiddle.net/mrcarllister/Z2GjN/
基本上,当您单击并拖动锚点/链接时,我点击拖动滚动工作除外。
似乎只有当鼠标光标停留在释放链接时才会跟踪网址(例如,如果你拖得足够远而且链接不在窗口,那就可以了。)
;(function($){
$.fn.scrollsync = function( options ){
var settings = $.extend(
{
targetSelector:':first',
axis: 'xy'
},options || {});
function scrollHandler(event) {
if (event.data.xaxis){
event.data.followers.scrollLeft(event.data.target.scrollLeft());
}
if (event.data.yaxis){
event.data.followers.scrollTop(event.data.target.scrollTop());
}
}
settings.target = this.filter(settings.targetSelector).filter(':first');
settings.followers=this.not(settings.target); // the rest of elements
settings.xaxis= (settings.axis=='xy' || settings.axis=='x') ? true : false;
settings.yaxis= (settings.axis=='xy' || settings.axis=='y') ? true : false;
if (!settings.xaxis && !settings.yaxis) return;
settings.target.bind('scroll', settings, scrollHandler);
};
})( jQuery );
;(function($){
$.fn.dragscrollable = function( options ){
var settings = $.extend(
{
dragSelector:'>:first',
acceptPropagatedEvent: true,
preventDefault: true
},options || {});
var dragscroll= {
mouseDownHandler : function(event) {
if (event.which!=1 ||
(!event.data.acceptPropagatedEvent && event.target != this)){
return false;
}
event.data.lastCoord = {left: event.clientX, top: event.clientY};
$.event.add( document, "mouseup",
dragscroll.mouseUpHandler, event.data );
$.event.add( document, "mousemove",
dragscroll.mouseMoveHandler, event.data );
if (event.data.preventDefault) {
event.preventDefault();
return false;
}
},
mouseMoveHandler : function(event) {
var delta = {left: (event.clientX - event.data.lastCoord.left),
top: (event.clientY - event.data.lastCoord.top)};
event.data.scrollable.scrollLeft(
event.data.scrollable.scrollLeft() - delta.left);
event.data.scrollable.scrollTop(
event.data.scrollable.scrollTop() - delta.top);
event.data.lastCoord={left: event.clientX, top: event.clientY}
if (event.data.preventDefault) {
event.preventDefault();
return false;
}
},
mouseUpHandler : function(event) {
$.event.remove( document, "mousemove", dragscroll.mouseMoveHandler);
$.event.remove( document, "mouseup", dragscroll.mouseUpHandler);
if (event.data.preventDefault) {
event.preventDefault();
return false;
}
}
}
this.each(function() {
var data = {scrollable : $(this),
acceptPropagatedEvent : settings.acceptPropagatedEvent,
preventDefault : settings.preventDefault }
$(this).find(settings.dragSelector).
bind('mousedown', data, dragscroll.mouseDownHandler);
});
};
})( jQuery );
我很确定我必须'返回:假;'对于锚点,但我不知道如何在拖动/滚动时如何做。
任何帮助都将不胜感激。
编辑 - Ian的建议有效,但我只需要几个补救措施:
--->>>>>>>>>>>>>
干杯,
卡尔
答案 0 :(得分:2)
好吧问题就是这里使用鼠标上下工作,所以你可以永久地禁用锚点,或者它可以工作,所以你需要有一些业务逻辑来说明它何时开启和关闭。如果你想编辑dragscrollable,把它放在代码中就行了
我添加了
event.data.initCoord = event.data.lastCoord
到mouseDownHandler,我添加了
if(event.data.lastCoord != event.data.initCoord){
obj = $(this);
$('a', obj).on('click', function(event) {
event.preventDefault();
});
setTimeout(function(){$('a', obj).off('click'); },300);
}
到mouseUpHandler,我打开了preventdefault
$('#viewport, #inner').
dragscrollable({dragSelector: '.dragger:first', preventDefault: true});
答案 1 :(得分:1)
基于你的例子,这似乎适用于小提琴。
<head>
<title>iPhone Test</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function() {
// Set drag scroll on first descendant of class dragger on both selected elements
$('.dragger').
draggable({ start:function(){ console.log("HI");
}});
});
</script>
</head>