jquery 拖DIV 问题现在已经实现这个功能 但是有个问题 能拖到浏览器外边了(看不见了)
想改进一下 当拖到浏览器边缘时 就不能再往外拖了 求改进jquery现在代码如下:jquery var _move = false; //移动标记
var _x, _y; //鼠标离控件左上角的相对位置
$(document).ready(function () {
$("#D").click(function () {
//alert("click");//点击(松开后触发)
}).mousedown(function (e) {
_move = true;
_x = e.pageX - parseInt($("#D").css("left"));
_y = e.pageY - parseInt($("#D").css("top"));
$("#D").fadeTo(20, 0.25); //点击后开始拖动并透明显示
});
$(document).mousemove(function (e) {
if (_move) {
var x = e.pageX - _x; //移动时根据鼠标位置计算控件左上角的绝对位置
var y = e.pageY - _y;
$("#D").css({ top: y, left: x }); //控件新位置
}
}).mouseup(function () {
_move = false;
$("#D").fadeTo("fast", 1); //松开鼠标后停止移动并恢复成不透明
});
});HTML<div class="t" id="D">可拖动</div>CSS.t{cursor:move;z-index:999999;height:22px; text-align:right;font-family:Arial;margin-top:-15px;border:1px solid #CCCCCC; position: fixed; left:750px; top:47px;}
想改进一下 当拖到浏览器边缘时 就不能再往外拖了 求改进jquery现在代码如下:jquery var _move = false; //移动标记
var _x, _y; //鼠标离控件左上角的相对位置
$(document).ready(function () {
$("#D").click(function () {
//alert("click");//点击(松开后触发)
}).mousedown(function (e) {
_move = true;
_x = e.pageX - parseInt($("#D").css("left"));
_y = e.pageY - parseInt($("#D").css("top"));
$("#D").fadeTo(20, 0.25); //点击后开始拖动并透明显示
});
$(document).mousemove(function (e) {
if (_move) {
var x = e.pageX - _x; //移动时根据鼠标位置计算控件左上角的绝对位置
var y = e.pageY - _y;
$("#D").css({ top: y, left: x }); //控件新位置
}
}).mouseup(function () {
_move = false;
$("#D").fadeTo("fast", 1); //松开鼠标后停止移动并恢复成不透明
});
});HTML<div class="t" id="D">可拖动</div>CSS.t{cursor:move;z-index:999999;height:22px; text-align:right;font-family:Arial;margin-top:-15px;border:1px solid #CCCCCC; position: fixed; left:750px; top:47px;}
var _x, _y; //鼠标离控件左上角的相对位置
var width = $(window).width(); //浏览器的宽
var height = $(window).height();//浏览器的高度
$(document).ready(function () {
$("#D").click(function () {
// alert("click"); //点击(松开后触发)
}).mousedown(function (e) {
_move = true;
_x = e.pageX - parseInt($("#D").css("left"));
_y = e.pageY - parseInt($("#D").css("top"));
$("#D").fadeTo(20, 0.25); //点击后开始拖动并透明显示
});
$(document).mousemove(function (e) {
if (_move) {
var x = e.pageX - _x; //移动时根据鼠标位置计算控件左上角的绝对位置
var y = e.pageY - _y;
var divWidth = $("#D").width();
var divHeight = $("#D").height();
if (x + divWidth <= width && y + divHeight <= height&&x>=0&&y>=0) {
$("#D").css({ top: y, left: x }); //控件新位置
}
}
}).mouseup(function () {
_move = false;
$("#D").fadeTo("fast", 1); //松开鼠标后停止移动并恢复成不透明
});
});
就是 当窗口化浏览器时 这个DIV不见了....
放大浏览器时 它又有了..
怎么让它 窗口化浏览器时 还在?
$(function()
{
$('#owindows').draggable({ containment: 'parent' });
var containment = $('#owindows').draggable('option', 'containment');
$('#owindows').draggable('option', 'containment', 'parent'); });这是我开发一个项目的jquery 跟你说的效果是一样的,
或者你用样式控制