इस पोस्ट के लिए एक उत्कृष्ट शुरुआती बिंदु था मेरी मुद्दा। मैं ऊपर जवाब संयुक्त, और एक अन्य अपनी पृष्ठभूमि छवि के मूल आयाम हो रही से संबंधित (SO:How do I get background image size in jQuery?)
मेरी विशेष सेटअप के साथ
मैं background-size:cover
उपयोग कर रहा था तो मैं पृष्ठभूमि छवि की ऊंचाई/चौड़ाई का अनुपात का निर्धारण और तुलना करने के लिए किया था यह कंटेनर की ऊंचाई/चौड़ाई के अनुपात के लिए। इसका मतलब है कि प्रत्येक छवि को केवल एक आयाम में स्लाइड करने की अनुमति है।
यहां दो अतिरिक्त सुविधाएं आपको आवश्यकता नहीं हो सकती है। मैंने उस तत्व को एक सीमा जोड़ा जो मैं वर्तमान में इसे देखने में आसान रखने के लिए छेड़छाड़ कर रहा था, जब मैंने किया था तो इसे हटा दिया। मैंने ऑफसेट मानों के साथ एक इनपुट भी अपडेट किया, इसलिए मैं उन्हें सर्वर पर भेज सकता था।
मुझे मेरी टिप्पणियों के साथ बहुत verbose लगता है, तो यह थोड़ा लंबा है, लेकिन मुझे उम्मीद है कि यह किसी की मदद करता है।
<script>
$(document).ready(function(){
function reset(){
start = {x: 0, y: 0};
$(this).css('backgroundPosition', '0 0');
}
$(".bg-img").bind('dblclick', reset);
//my jquery
$(".bg-img").on('mousedown mouseup', function(e){
//declare some vars
var start = {x: 0, y: 0};
var move = {x: 0, y: 0};
var id = $(this).attr('id');
//pointer coordinates on mousedown
var origin = {x: 0, y: 0};
//container dimensions
var container = {w: $(this).width(), h: $(this).height()};
//container ratio
var containerRatio = container.h/container.w;
//background image dimensions, note: this gets dimensions of unscaled image
var img = new Image;
img.src = $(this).css('background-image').replace(/url\(|\)$/ig, "");
var background = {w: img.width, h: img.height};
//background ratio
var backgroundRatio = background.h/background.w;
//max x and y position, aka boundary
var min = {x: 0, y: 0};
var max = {x: 0, y: 0};
//move x
if(backgroundRatio < containerRatio){
min.y = 0;
min.x = -((container.h * (1/backgroundRatio)) - container.w);
}
//move y
else if (backgroundRatio > containerRatio){
min.x = 0;
min.y = -((container.w * backgroundRatio) - container.h);
}
//ratios are equal, don't move anything
else{
min.x = 0;
min.y = 0;
}
//activate
if(e.type == 'mousedown'){
//add border so it's easier to visualize
$(this).css('border', '1px solid #000000');
//get current position of mouse pointer
origin.x = e.clientX;
origin.y = e.clientY;
//get current background image starting position
var temp = $(this).css('background-position').split(" ");
start.x = parseInt(temp[0]);
start.y = parseInt(temp[1]);
//mouse is dragged while mousedown
$(this).mousemove(function(e){
//move position
move.x = start.x + (e.clientX - origin.x);
move.y = start.y + (e.clientY - origin.y);
//if it's in the bounds, move it
if(move.x <= max.x && move.x >= min.x && move.y <= max.y && move.y >= min.y){
console.log('both');
//alter css
$(this).css('background-position', move.x + 'px ' + move.y + 'px');
//update input
$("#" + id).val('x:' + move.x + ', y:' + move.y);
}
//in x bound,
else if(move.x <= max.x && move.x >= min.x){
console.log('x');
//below min.y
if(move.y < min.y){
$(this).css('background-position', move.x + 'px ' + min.y + 'px');
//update input
$("#" + id).val('x:' + move.x + ', y:' + min.y);
}
//above max.y
else if(move.y > max.y){
$(this).css('background-position', move.x + 'px ' + max.y + 'px');
//update input
$("#" + id).val('x:' + move.x + ', y:' + max.y);
}
}
//in y bound
else if(move.y <= max.y && move.y >= min.y){
console.log('y');
//below min.x
if(move.x < min.x){
$(this).css('background-position', min.x + 'px ' + move.y + 'px');
//update input
$("#" + id).val('x:' + min.x + ', y:' + move.y);
}
//above max.x
else if(move.x > max.x){
$(this).css('background-position', max.x + 'px ' + move.y + 'px');
//update input
$("#" + id).val('x:' + max.x + ', y:' + move.y);
}
}
//out of both bounds
else{
console.log('problem');
}
});
}
//deactivate
else{
//remove border
$(this).css('border', 'none');
//remove mousemove
$(this).off('mousemove');
$(document.body).focus();
}
});
});
</script>
बस कुछ बाहर निकालने के लिए, मैं 'ऑनमोसडाउन' को 'ऑनमूसोम' हैंडलर को आंदोलन का पता लगाने और पंजीकरण करने और 'div' की' पृष्ठभूमि-छवि-स्थिति 'सेटिंग्स को रीसेट करने के लिए सेट करता हूं, और फिर' mousemove 'को' अनबिंड ' 'onmouseup'। उचित लगता है? –
मैं यही प्रश्न लिखने के बाद से कर रहा हूं :) – yoda
यदि आपको कुछ काम मिल रहा है, तो इसे उत्तर के रूप में पोस्ट करें। मुझे http://jsfiddle.net काम करने वाले डेमो को देखने में दिलचस्पी होगी। ':)' –