My image gallery has next and previous links, when the user click on either one it loads an image in the #the_image div, but I need the image resize everytime. My thinking I would put the code to resize the image as a callback function for the .load. But's it doesn't work everytime. Odd thing also, if I put an alert("blah"); after image = $('#the_image'); the image get's positioned correctly every time. Any ideas?
Here's my JS:
<script type="text/javascript">
$(document).ready(function() {
$('#previous').live('click', function(e){
$('#img_wrapper').load( $(this).attr('href') + ' #container',function () {
var height = $(window).height();
var width = $(document).width();
var image = $('#the_image');
$("#the_image").css("max-height",height-200);
image.css({
'max-height': height-200,
'left': width/2 - (image.width() /2)-5,
'top': height/2 -(image.height() /2),
});
prev = $('#previous'); next = $('#next');
prev.css({
'left': 0,
'top': (height/2)-150,
});
next.css({
'left': 924,
'top': (height/2)-150,
});
});
e.preventDefault();
});
});
</script>
This is what works. Thanks to Jasper, had to modify his code a bit:
$(function() {
$(document).delegate('#next', 'click', function (e) {
$('#img_wrapper').load( $(this).attr('href') + ' #container',function () {
var height = $(window).height(),
width = $(document).width();
$("#the_image").bind('load', function() {
var image = $('#the_image');
$("#the_image").css("max-height",height-200);
image.css({
'max-height': height-200,
'left': width/2 - (image.width() /2)-5,
'top': height/2 -(image.height() /2),
});
});
$('#previous').css({
'left' : 0,
'top' : ((height / 2) - 150),
});
$('#next').css({
'left' : 924,
'top' : ((height / 2) - 150),
});
});
e.preventDefault();
});
});
The image has to load before you can get it's width/height.
$(function() {
$(document).delegate('#previous', 'click', function (e) {
$('#img_wrapper').load( $(this).attr('href') + ' #container',function () {
var height = $(window).height(),
width = $(document).width();
//here's the fix, we wait for the image to load before changing it's dimensions
$("#the_image").bind('load', function (
$(this).css({
'max-height' : (height - 200),
'left' : (width / 2 - ($(this).width() / 2) - 5),
'top' : (height / 2 - ($(this).height() / 2)),
});
});
$('#previous').css({
'left' : 0,
'top' : ((height / 2) - 150),
});
$('#next').css({
'left' : 924,
'top' : ((height / 2) - 150),
});
});
e.preventDefault();
});
});
Alternatively I would use a different AJAX function so you can add the load event handler to the image before it is added to the DOM (to make sure you can capture the load event before it fires):
$(function() {
$(document).delegate('#previous', 'click', function (e) {
$.get($(this).attr('href'), function (serverResponse) {
var height = $(window).height(),
width = $(document).width();
//here's the fix, we wait for the image to load before changing it's dimensions
serverResponse = $(serverResponse).find('#container');
serverResponse.find('#the_image').bind('load', function (
$(this).css({
'max-height' : (height - 200),
'left' : (width / 2 - ($(this).width() / 2) - 5),
'top' : (height / 2 - ($(this).height() / 2)),
});
});
//add the new HTML to the DOM, `.load()` did this automatically
$('#img_wrapper').html(serverResponse);
$('#previous').css({
'left' : 0,
'top' : ((height / 2) - 150),
});
$('#next').css({
'left' : 924,
'top' : ((height / 2) - 150),
});
});
e.preventDefault();
});
});
Try this, and remember height and width are properties not methods:
Also, don't store objects into variables unless you're going to do more work than dot chaining only once per event fire. Oh and I also forgot... the +"px" will really help for IE!
<script type="text/javascript">
$(document).ready(function() {
$('body #main #wrapper').on('click', '#previous' function(e){
$('#img_wrapper').find("img").attr("src", $(this).attr('href') + ' #container');
var height = $(window).height(), width = $(document).width();
$('#the_image').css({
'max-height': parseInt((height-200), 10)+"px",
'left': parseInt((width/2 - (image.width /2)-5), 10)+"px",
'top': parseInt((height/2 -(image.height /2)), 10)+"px",
});
$('#previous')css({
'left': 0,
'top': parseInt( ((height/2)-150), 10)+"px",
});
$('#next'.css({
'left': 924+"px",
'top': parseInt( ((height/2)-150), 10)+"px",
});
});
e.preventDefault();
});
});
</script>
Related
I am working on drag and drop of images.my drag and drop is working fine.when i drag any image from sidebar and drop it then it shows a control box with dragged item and all this is working fine. Now i need to do the same functionality on click or double click of a image.when user will click any image in sidebar then that image will be automatically dropped in the dropable div area. This is my drag and drop code. Do i need to do same code again on double click of an image?
jQuery(document).ready(function ()
{
dragablediv();
});
function dragablediv()
{
var x = null;
jQuery(".get-image .drag").draggable({
helper: 'clone',
cursor: 'move',
tolerance: 'fit',
revert: true,
/* stop: function (event, ui) { ... } */
});
jQuery(".droppable").droppable({
accept: '.drag',
activeClass: "drop-area",
drop: function (e, ui) {
if (jQuery(ui.draggable)[0].id != "") {
x = ui.helper.clone();
ui.helper.remove();
jQuery("#droppable .resize-img").css({"display":"inline-table"});
/* To show Controls first time on Dragged Image*/
var droppedItemId = ui.draggable.attr("data-item-id");
jQuery(".controls ").attr("controls-item-id",droppedItemId);
jQuery("#droppable").find(".ui-resizable-handle").hide(); // To show Controls
jQuery(".controls").show();
dragEl = jQuery(this);
stopPosition = dragEl.position();
var height = x.height(); // To show the controls with exact position
var width = x.width(); //alert("stoppos"+stopPosition+"leftpos"+leftPos);
var topPos = ui.position.top;
var leftPos = ui.position.left;
var mytop = topPos+height;
var myleft = leftPos + width /2;
//alert("left"+leftPos);
jQuery(".controls").css({"top": mytop , "left":myleft, "display":"block"});
x.draggable({
helper: 'original',
cursor: 'move',
containment: '#droppable',
tolerance: 'fit',
drop: function (event, ui) {
jQuery(ui.draggable).remove();
},
drag: function() {
},
stop: function() {
dragEl = jQuery(this);
stopPosition = dragEl.position();
var leftPos = dragEl.position().left;
var topPos = dragEl.position().top;
stopOffset = dragEl.offset();
document_id = dragEl.data("document-id");
signer_id = dragEl.data("signer-id");
var height = x.height();
var width = x.width();
var mytop = stopPosition.top+height;
//var myleft = stopPosition.left + width /2;
jQuery(".controls").css({"top": mytop , "left":myleft, "display":"block"});
dataItem = jQuery(this).attr('data-item-id');
jQuery("#droppable").find(".ui-resizable-handle").hide();
jQuery(".controls").hide();
jQuery(this).closest(".drag").find(".ui-resizable-handle").show();
jQuery(".controls").show();
jQuery(".controls ").attr("controls-item-id",dataItem);
}
});
x.resizable({
maxHeight: jQuery('#droppable').height(),
maxWidth: jQuery('#droppable').width(),
aspectRatio: true,
helper: "ui-resizable-helper",
handles: "nw, ne, sw,se"
});
//x.addClass('remove');
x.appendTo('.droppable');
}
}
});
}
So I have the code:
header("Location: ".$config['site_url']."index.php#login-box");
Which is my attempt to trigger the html code for a jquery login form if the user is not logged in:
<li><a class="log" href="#login-box"><span class="hover" style="opacity: 0;"></span></a></li>
But when it runs all it does is add #login-box to then end of my url and doesn't trigger the #login-box href. Pretty lame attempt I know, but any ideas?
This is the jquery script, how would I add the code you mentioned to it properly?
$(document).ready(function() {
$('a.login-window2').click(function() {
//Getting the variable's value from a link
var loginBox = $(this).attr('href');
//Fade in the Popup
$(loginBox).fadeIn(300);
//Set the center alignment padding + border see css style
var popMargTop = ($(loginBox).height() + 24) / 2;
var popMargLeft = ($(loginBox).width() + 24) / 2;
$(loginBox).css({
'margin-top' : -popMargTop,
'margin-left' : -popMargLeft
});
// Add the mask to body
$('body').append('<div id="mask"></div>');
$('#mask').fadeIn(300);
return false;
});
// When clicking on the button close or the mask layer the popup closed
$('a.close, #mask').live('click', function() {
$('#mask , .login-popup').fadeOut(300 , function() {
$('#mask').remove();
});
return false;
});
});
</script>
Here is a little snippet that should do the trick, using jQuery as you mentioned:
$(function(){
$("a.login-window2").click(function(){
//Getting the variable's value from a link
var loginBox = $(this).attr("href");
//Fade in the Popup
$(loginBox).fadeIn(300);
//Set the center alignment padding + border see css style
var popMargTop = ($(loginBox).height() + 24) / 2;
var popMargLeft = ($(loginBox).width() + 24) / 2;
$(loginBox).css({
"margin-top" : -popMargTop,
"margin-left" : -popMargLeft
});
// Add the mask to body
$("body").append("<div id=\"mask\"></div>");
$("#mask").fadeIn(300);
return(false);
});
// When clicking on the button close or the mask layer the popup closed
$("a.close, #mask").live("click", function(){
$("#mask , .login-popup").fadeOut(300, function(){
$("#mask").remove();
});
return(false);
});
// Check the hash
var hash = $(location).attr("hash");
if(hash == "#login-box") {
$("a.login-window2").click();
}
});
Put this in your index.php file and run your code from inside the if statement.
It's a mistake in your code.
Use: name="#login-box"
instead of: href="#login-box"
http://hollywoodlacewigs.netsmartz.us/
I am working on above project.
user upload pic form 'upload' button and then 'select area' and then click 'mix' button.
this will show the cropped area onto other image.
click email link will compose two images.
But before that I want to let user 'rotate', 'zoom in/out' image before cropping the image.
am using jcrop, jquery ui draggable
here's the script :
$(document).ready(function() {
$('#UploadForm').change('submit', function(e) {
e.preventDefault();
$('#SubmitButton').attr('disabled', ''); // disable upload button
$('#left-pic').hide();
$('#output').show();
//show uploading message
$("#output").html('<img style="margin:45px 0px 0px 35px; " src="images/ajax-loader.gif" alt="Please Wait"/>');
$(this).ajaxSubmit({
target: '#output',
success: afterSuccess //call function after success
});
});
});
function afterSuccess(){
$('#UploadForm').resetForm(); // reset form
$('#SubmitButton').removeAttr('disabled'); //enable submit button
$('#cropbox').Jcrop({
aspectRatio: 1,
onSelect: updateCoords,
bgOpacity: 0.5,
bgColor: 'white',
addClass: 'jcrop-dark',
setSelect: [ 34, 50, 134, 155 ]
});
}
function updateCoords(c){
$('#x').val(c.x);
$('#y').val(c.y);
$('#w').val(c.w);
$('#h').val(c.h);
}
function checkCoords(){
if (parseInt($('#w').val())) return true;
alert('Please select a crop region then press submit.');
return false;
}
$(function() {
$('#draggable3').draggable({
containment: $('#draggable3').parent(),
cursor: "move", cursorAt: { top: 56, left: 56 },
drag: function(event, ui) {
// Show the current dragged position of image
var currentPos = $(this).position();
// $("#xpos").text("CURRENT: \nLeft: " + (currentPos.left-580) + "\nTop: " + (currentPos.top-82));
$('#x3').val(currentPos.left-580);
$('#y3').val(currentPos.top-82);
}
});
});
You can use http://www.cropzoom.com.ar/demo/, I used and like it.
In IE exists one bug - fix is described on Draggable element in VML get stuck when size change (height, width) in IE 8
(I want to add answer in comment, but don't have permission)
I am building a webshop in magento.
On the product detailpage i am resizing the image in Magento itself so that it doesn't have to load the very big image.
When i click on the image it should open a lightbox with the image but this time it has to be larger.
But because i resized it the quality really is really low.
There is one other thing which i can't seem to figure out.
I also have some smaller images which should appear as main image when they are hovered over. If i hover over them my resize doesn't work anymore.
I hope someone can point out my mistakes :)
Here is my code:
HTML/PHP code for getting and resizing the image.
<p class="product-image">
<?php
$_img = '<img id="image1" src="'.$this->helper('catalog/image')->init($_product, 'image')->resize(235, 350).'" alt="'.$this->htmlEscape($this->getImageLabel()).'" title="'.$this->htmlEscape($this->getImageLabel()).'" width="235" height="350 " />';
echo $_helper->productAttribute($_product, $_img, 'image');
?>
Javascript code for switching the images on hover.
function imageswitcher(imagename){
jQuery('#image1').attr('src', imagename);
var options = {
zoomrange: [<?php echo Mage::getStoreConfig('featurezoom/zoomrange/start');?>, <?php echo Mage::getStoreConfig('featurezoom/zoomrange/end');?>],
magnifiersize: [<?php echo Mage::getStoreConfig('featurezoom/magnifiersize/height');?>,<?php echo Mage::getStoreConfig('featurezoom/magnifiersize/width');?>],
magnifierpos: '<?php echo Mage::getStoreConfig('featurezoom/general/magnifierpos');?>',
cursorshade: true,
largeimage: imagename //<-- No comma after last option!
}
jQuery('#image1').addimagezoom(options);
}
function doTimer(imageName){
myTimer=setTimeout(function(){
imageswitcher(imagename);
}, 2000);
}
Event.observe(window, 'load', function() {
jQuery('#image1').addimagezoom({
zoomrange: [<?php echo Mage::getStoreConfig('featurezoom/zoomrange/start');?>, <?php echo Mage::getStoreConfig('featurezoom/zoomrange/end');?>],
magnifiersize: [<?php echo Mage::getStoreConfig('featurezoom/magnifiersize/height');?>,<?php echo Mage::getStoreConfig('featurezoom/magnifiersize/width');?>],
magnifierpos: '<?php echo Mage::getStoreConfig('featurezoom/general/magnifierpos');?>',
cursorshade: true,
largeimage: '<?php echo $this->helper('catalog/image')->init($_product, 'image');?>' //<-- No comma after last option!
})
});
jQuery code for opening positioning and closing the lightbox.
(function(window, $, undefined) {
jQuery(function() {
/*$(window).on('click', function(e) {
console.log(e.target);
//e.preventDefault();
});*/
$('.zoomtracker').on('click', function(e) {
removeLightbox();
$('body').css('overflow-y', 'hidden'); // hide scrollbars!
$('<div id="overlay"></div>')
.css('top', $(document).scrollTop())
.css('opacity', '0')
.appendTo('body');
$('<div id="lightbox"></div>')
.hide()
.appendTo('body');
var img = new Image();
img.onload = function() {
var width = img.width,
height = img.height;
$('<img id="lightboxImage" />')
.attr('width', '235')
.attr('src', img.src)
.load(function() {
positionLightboxImage(e);
})
.click(function() {
removeLightbox();
})
.appendTo('#lightbox');
};
img.src = $('#image2').attr('src');
$('#overlay').click(function(e){
})
/*var height = $('<img />').attr('width', .width());
alert(height);
$('<img />')
.attr('src', $(this).attr('href'))
.load(function() {
positionLightboxImage(e);
})
.click(function() {
removeLightbox();
})
.appendTo('#lightbox');
return false;*/
e.preventDefault();
});
});
function positionLightboxImage(e) {
var top = 173;
var left = 271;
/* In plaats van de img width en height de hoogte en breedte van het scherm berekenen en aan de hand heirvan het centrum bepalen. */
$('#lightbox')
.css({
'top': top,
'left': left
})
.fadeIn(500)
.animate({'width': '640','left': '50%', 'margin-left': -($('#lightbox').width()/2), 'top': '19%', 'margin-top': -($('#lightbox').height()/2)}, {queue: false}, 8000);
$('#lightboxImage')
.animate({'width': '550','height': '930'}, {queue: false}, 8000)
}
function removeLightbox() {
$('#overlay, #lightbox')
.fadeOut(500, function() {
$(this).remove();
$('body').css('overflow-y', 'auto'); // show scrollbars!
})
.animate({'width': '235','left': '-72', 'margin-left': ($('#lightbox').width()/2), 'top': '-295', 'margin-top': ($('#lightbox').height()/2)}, {queue: false}, 8000);
$('#lightboxImage')
.animate({'width': '235', 'height': '350'}, {queue: false}, 8000)
}
jQuery.fn.center = function () {
this.css("position","absolute");
this.css("top", Math.max(0, (($(window).height() - this.outerHeight()) / 2) +
$(window).scrollTop()) + "px");
this.css("left", Math.max(0, (($(window).width() - this.outerWidth()) / 2) +
$(window).scrollLeft()) + "px");
return this;
}
})(window, jQuery);
Try to use naturalWidth and naturalHeight methods after image load and your scripts initialised.
jsFiddle example.
var img = $("img")[0];
alert( img.naturalWidth + "x" + img.naturalHeight );
Note: if you wan to do this with another way, you can inspect this answer too: https://stackoverflow.com/a/670433/1428241
I have a small problem. i have a link and when i click on this link i want an image to grow to a certain size and move to a certain spot on the screen. Everthing goes fine except that the image pops back to it's original size after the animation is complete.
My project is in Magento so sorry about the weird display of the image
PS: In the img tag it says width="235" height="401".
But the image pops to width=924" and height="1379". These are the dimensions of the actual image.
Here is my HTML code:
<a class="lightbox"href="#">pic</a>
<p class="product-image">
<?php
$_img = '<img id="image1" src="'.$this->helper('catalog/image')->init($_product, 'image').'" alt="'.$this->htmlEscape($this->getImageLabel()).'" title="'.$this->htmlEscape($this->getImageLabel()).'" width="235" height="401 " />';
echo $_helper->productAttribute($_product, $_img, 'image');
?>
And here is my jQuery code:
(function(window, $, undefined) {
jQuery(function(){
$('.lightbox').click(function(e) {
$('body').css('overflow-y', 'hidden'); // hide scrollbars!
var clicked = $('.lightbox').position();
$('<div id="overlay"></div>')
.css('top', $(document).scrollTop())
.css('opacity', '0')
.animate({'opacity': '0.5'}, 'slow')
.appendTo('body');
$('<div id="lightbox"></div>')
.hide()
.appendTo('body');
var img = new Image();
img.onload = function() {
var width = img.width,
height = img.height;
$('<img />')
.attr('src', img.src)
.load(function() {
positionLightboxImage(e, width, height);
})
.click(function() {
removeLightbox();
})
.appendTo('#lightbox');
};
img.src = $('#image1').attr('src');
/*var height = $('<img />').attr('width', .width());
alert(height);
$('<img />')
.attr('src', $(this).attr('href'))
.load(function() {
positionLightboxImage(e);
})
.click(function() {
removeLightbox();
})
.appendTo('#lightbox');
return false;*/
e.preventDefault();
});
});
function positionLightboxImage(e, width, height) {
var top = e.pageY - (height / 2);
var left = e.pageX - (width / 2);
$('#lightbox')
.css({
'top': top,
'left': left
})
.fadeIn(1)
.animate({'width': '50px'}, 3000);
}
function removeLightbox() {
$('#overlay, #lightbox')
.fadeOut('slow', function() {
$(this).remove();
$('body').css('overflow-y', 'auto'); // show scrollbars!
});
}
jQuery.fn.center = function () {
this.css("position","absolute");
this.css("top", Math.max(0, (($(window).height() - this.outerHeight()) / 2) +
$(window).scrollTop()) + "px");
this.css("left", Math.max(0, (($(window).width() - this.outerWidth()) / 2) +
$(window).scrollLeft()) + "px");
return this;
}
})(window, jQuery);
I hope i gave enough information. :)
For anyone who is interested in this answer.
It isn't the best way to fix this.
But using a smaller image for the lightbox and keeping the big image in the data-src did the trick for this situation.