Ich versuche, ein Bild von einem bestimmten LinkBild laden mit jQuery und hängen Sie es an die DOM
var imgPath = $(imgLink).attr('href');
und hängen Sie ihn an die Seite zu laden, so kann ich es in einem gegebenen einfügen Element für einen Bildbetrachter. Obwohl ich Stackoverflow und die jQuery Dokumente ohne Ende suchte, kann ich es nicht herausfinden.
Nachdem ich das Bild zu laden, möchte ich verschiedene Werte, um es zu setzen, wie Breite, Höhe usw.
Update:
Dies ist, was ich bekam. Das Problem, das ich habe, ist, dass ich jQuery Funktionen auf dem img
Element nicht ausführen kann.
function imagePostition(imgLink) {
// Load the image we want to display from the given <a> link
// Load the image path form the link
var imgPath = $(imgLink).attr('href');
// Add image to html
$('<img src="'+ imgPath +'" class="original">').load(function() {
$(imgLink).append(this);
var img = this;
// Resize the image to the window width
// http://stackoverflow.com/questions/1143517/jquery-resizing-image
var maxWidth = $(window).width(); // window width
var maxHeight = $(window).height(); // window height
var imgWidth = img.width; // image width
var imgHeight = img.height; // image height
var ratio = 0; // resize ration
var topPosition = 0; // top image position
var leftPostition = 0; // left image postiton
// calculate image dimension
if (imgWidth > maxWidth) {
ratio = imgHeight/imgWidth;
imgWidth = maxWidth;
imgHeight = (maxWidth * ratio);
}
else if (imgHeight > maxHeight) {
ratio = imgWidth/imgHeight;
imgWidth = (maxHeight * ratio);
imgHeight = maxHeight;
}
// calculate image position
// check if the window is larger than the image
// y position
if(maxHeight > imgHeight) {
topPosition = (maxHeight/2) - (imgHeight/2);
}
// x position
if(maxWidth > imgWidth) {
leftPostition = (maxWidth/2) - (imgWidth/2);
}
$(imgLink).append(img);
// Set absolute image position
img.css("top", topPosition);
img.css("left", leftPostition);
// Set image width and height
img.attr('width', imgWidth);
img.attr('height', imgHeight);
// Add backdrop
$('body').prepend('<div id="backdrop"></div>');
// Set backdrop size
$("#backdrop").css("width", maxWidth);
$("#backdrop").css("height", maxHeight);
// reveal image
img.animate({opacity: 1}, 100)
img.show()
});
};
Wenn ich dann Eigenschaften aus dem Bild, wie seine Höhe und Breite, erhalten möchte, wie kann ich es bekommen? '$ ('') .load (function() {var img = $ (this); ...' scheint nicht zu funktionieren – wowpatrick
Können wir aufhören, den Elefanten im Raum zu ignorieren - imgPath wird falsch geschrieben – sq2
@ sq2..hi mate ... check den Elefanten 'imgPaht' in OPs Beitrag und kommentiere .. thx':) ' – thecodeparadox