我想構建一個簡單的圖像「點擊拖拽」調整大小的腳本。我不想使用jQuery UI,因爲它添加了很多額外的div,使文本混亂,並需要額外的步驟來刪除這些額外的div。問題與自定義拖動和調整大小腳本不平滑
這個腳本的作品,但是你會發現在小提琴中,它是非常跳動。
我一直在試圖弄清楚如何獲得它,以便它只在鼠標移動時調整大小,只有當鼠標「下」(單擊)並且在mouseup時,停止調整大小並將變量重置回原始起始位置。
在此先感謝。對不起,這裏草率的格式,更乾淨的小提琴。
得到了原代碼從這裏:originalsource 我粘貼原來如此,你可以得到它
這裏清晰的畫面是的jsfiddle:jsfiddle
jQuery.fn.extend({
resize: function(params){
var jQ = jQuery;
return this.each(function(){
var clicked = false; //set to off
var start_x; //starting point of mouse
var start_y;
if(params && params['target']){ var resize = params['target'];} //if target passed then use that
else{ var resize = this; }
if(params && typeof(params['y']) != "undefined"){ var y = params['y'];} //if y passed then fix the max height
else{ var y = 1;}
if(params && typeof(params['x']) != "undefined"){ var x = params['x'];} //if x then fix width
else{ var x = 1;}
if(params && typeof(params['min_width']) != "undefined"){ var min_w = params['min_width'];}
else{ var min_w = 1;}
if(params && typeof(params['min_height']) != "undefined"){ var min_h = params['min_height'];}
else{ var min_h = 1;}
$(this).hover(
function(){$(this).css('cursor', 'move');},
function(){$(this).css('cursor','default');clicked=false;}
);
$(this).mousedown(function(e){
clicked = true;
start_x = Math.round(e.pageX - $(this).eq(0).offset().left);
start_y = Math.round(e.pageY - $(this).eq(0).offset().top);
});
$(this).mouseup(function(e){clicked = false;});
$(this).mousemove(function(e){
if(clicked){
var mouse_x = Math.round(e.pageX - $(this).eq(0).offset().left) - start_x;
var mouse_y = Math.round(e.pageY - $(this).eq(0).offset().top) - start_y;
var div_w = $(resize).width();
var div_h = $(resize).height();
var new_w = parseInt(div_w)+mouse_x;
var new_h = parseInt(div_h)+mouse_y;
if(x==1 || (typeof(x) == "number" && new_w < x && new_w > min_w)){ $(resize).css('width', new_w+"px"); }
if(y==1 || (typeof(y) == "number" && new_h < y && new_h > min_h)){ $(resize).css('height',new_h+"px"); }
start_x = Math.round(e.pageX - $(this).eq(0).offset().left);
start_y = Math.round(e.pageY - $(this).eq(0).offset().top);
}
});
});
}
});
是的,這是一個很好的電話。你有沒有注意到鼠標移動的問題?如果將鼠標從圖像的左上角移到圖像的右下角,然後將鼠標移回左上角,則圖像不會完全恢復爲原始大小。 –
@K_G檢查此:http://jsfiddle.net/a3vYu/13/少跳躍是不是? – jtheman
是啊,男人看起來不錯,我沒有試圖重新創建jQuery UI,只是想要更簡單的東西。謝啦! –