[英]Resize an html element using mouse - jQuery
I have container and I want to resize it with mouse using JavaScript. 我有容器,我想使用JavaScript使用鼠标来调整它的大小。 the implementation uses mousedown , mouseup , and mousemove events but I have slight problem when page is already scrolled before the mousedown is fired. 该实现使用mousedown , mouseup和mousemove事件,但是在触发mousedown之前已经滚动页面时,我有一个小问题。
the whole page seems to scroll to position zero. 整个页面似乎滚动到位置零。 How can i fix it so whether page is already scrolled or not the resize will work fine. 我如何解决它,以便无论页面是否已经滚动或调整大小都可以正常工作。
JS: JS:
//resize div vertically or horizontal or both
$.fn.resizeMe = function(options){
var grippie = $(this),
options = $.extend({resizeMe:"",resize:"vertical"},options),
resizeMe = $(options.resizeMe);
grippie.on('mousedown',function(e){initialiseGrippieResize(e)});
function initialiseGrippieResize(e) {
$(window).on('mousemove',function(e){
startResizing(e);
resizeMe.css({opacity:.25});
}).on('mouseup',function(e){
stopResizing(e);
resizeMe.css({opacity:1});
});
}
//css objects
function cssOBJ(e,key){
var css = {
vertical:{height:(e.clientY - resizeMe.offset().top)},
horizontal : {width:(e.clientX - resizeMe.offset().left)},
both: {
height:(e.clientY - resizeMe.offset().top),
width: (e.clientX - resizeMe.offset().left)
}
};
//return objects
return css[key];
}
//Start Resizing
function startResizing(e) {
resizeMe.css(cssOBJ(e,options.resize));
}
function stopResizing(e) {
$(window).off('mousemove mouseup');
}
}
$('.grippie').resizeMe({resizeMe:"#pane",resize:'vertical'});
HTML: HTML:
<div id='main-container'>
<div id='pane' arial-lable='content-wrapper'>contents will go here</div>
<div class='grippie'></div>
</div>
CSS: CSS:
#pane{
resize: n-resize;
overflow: hidden;
border: 1px solid #d6d9dc;
padding:15px 20px;
min-height:50px;
}
#pane *{border:0px !important; background: #fff !important;}
.grippie {
background-position: center;
border: 1px solid #d6d9dc;
border-width: 0 1px 1px;
cursor: s-resize;
height: 9px;
overflow: hidden;
background-color: #eff0f1;
/*background-image: url('images/icons.png');*/
background-repeat: no-repeat;
In JavaScript 在JavaScript中
pageX , pageY , screenX , screenY , clientX , and clientY returns a number which indicates the number of physical “CSS pixels” a point is from the reference point. pageX , pageY , screenX , screenY , clientX和clientY返回一个数字,该数字指示一个点相对于参考点的物理“ CSS像素”数目。 The event point is where the user moved the mouse, the reference point is a point in the upper left. 事件点是用户移动鼠标的位置,参考点是左上角的点。 These properties return the horizontal and vertical distance from that reference point. 这些属性返回到该参考点的水平和垂直距离。
but pageX or pageY returns a number which indicates the amount of page scrolled top or left. 但是pageX或pageY返回一个数字,该数字指示顶部或左侧滚动的页面数量。
so changing the clientX to pageX and clientY to pageY 因此将clientX更改为pageX,将clientY 更改为pageY
solved the problem and now I can resize any element. 解决了问题,现在我可以调整任何元素的大小。 :) :) :) :)
//css objects
function cssOBJ(e,key){
var css = {
vertical:{height:(e.pageY - resizeMe.offset().top)},
horizontal : {width:(e.pageX - resizeMe.offset().left)},
both: {
height:(e.pageY - resizeMe.offset().top),
width: (e.pageX - resizeMe.offset().left)
}
};
//return objects
return css[key];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.