添加条件测试,检查鼠标位置是否超出某个边界。如果鼠标超过固定限制 ( window width - element's width
),请不要更改right
标记的属性。
小提琴:http: //jsfiddle.net/ErWjr/1/
$(function () {
"use strict";
var box = $(".what-is-delicious"),
button = $(".what-is-delicious > a"),
mouseDown = false,
grabbed = 0,
start = -303,
maxLeftOffset = $(window).width() - 75;
// ^^^ Limit to 75px from the right (= marker's width)
button.mousedown(function (e) {
mouseDown = true;
$('*').bind('selectstart', false);
grabbed = e.pageX;
button.css({ cursor: "-moz-grabbing"});
$("body").append('<div class="background-overlay"></div>');
});
$('body').mouseup(function () {
mouseDown = false;
$('*').unbind('selectstart', false);
button.css({ cursor: "-moz-grab"});
$(".background-overlay").remove();
start = parseInt(box.css('right'), 10);
}).mousemove(function (e) {
if (mouseDown) {
if(e.pageX > maxLeftOffset) return; //<---- Don't go past an edge
//button.addClass("open");
box.css("right", Math.min(grabbed - e.pageX + start, 0));
}
});
});