PDA

View Full Version : jQuery Image Magnify v1.11



armasmike
06-07-2011, 04:38 PM
1) Script Title: jQuery Image Magnify v1.11

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex4/imagemagnify.htm

3) Describe problem:

is there a way to have the image de-magnified when a user click other image and or click outside the image.

I don't want to image to stay up if the user click some where other then the image to de-magnified it.

jscheuer1
06-07-2011, 05:12 PM
Try putting this script in the head of the page after the external script tags for jQuery and the magnifier:


<script type="text/javascript">
jQuery(function($){
$(document).click(function(e){
if(e.target.className === 'magnify'){return;}
$('.magnify').each(function(){
if(this.style.zIndex){$(this).click();}
});
});
});
</script>

Worked here in Firefox and IE on the demo page.

armasmike
06-07-2011, 05:42 PM
thats didn't work for me i have it in some php code and when i added


echo "<script type='text/javascript' src='js/jquery.min.js'></script>";

echo "<script type='text/javascript' src='js/jquery.magnifier.js'></script>";

echo "<script type='text/javascript'>";
echo "jQuery(function($){";
echo " $(document).click(function(e){";
echo " if(e.target.className === 'magnify'){return;}";
echo " $('.magnify').each(function(){";
echo " if(this.style.zIndex){$(this).click();}";
echo " });";
echo " });";
echo "});";
echo "</script>";


this to it. It made the page in IE display blank

jscheuer1
06-07-2011, 07:16 PM
That's no guarantee of what the served code of the page will be. View the page in the browser. Use the browser's 'view source'. Make sure the code looks like it's supposed to.

If you want more help:

Please post a link to a page on your site that contains the problematic code so we can check it out.

armasmike
06-08-2011, 06:33 PM
i got it to work by putting


echo "<script type='text/javascript' src='js/test.js'></script>";

jscheuer1
06-08-2011, 07:52 PM
Just as a follow up, in PHP when you echo strings quoted in double quotes, that allows for the resolution of variables, globals, etc. - all kinds of things, according to the rules/logic of PHP. Sometimes unexpected results occur. I just tried your code on my wamp server and got:



Parse error: parse error, expecting `T_VARIABLE' or `'$'' in test_script.php on line 7

Depending upon how one counts lines, that's one of these:


echo " $('.magnify').each(function(){";
echo " if(this.style.zIndex){$(this).click();}";
echo " });";

probably the middle one. The unquoted parenthetical preceded by a $ could be expected to resolve, I guess - PHP isn't my strong suit. But I'm sure its something at least vaguely like that.

Only by using single quotes are you assured of a literal echo. Also in javascript line breaks can be important. I don't think they are here. But it never hurts to play it safe. So you could have done:


echo '<script type="text/javascript">' . "\n";
echo 'jQuery(function($){' . "\n";
echo ' $(document).click(function(e){' . "\n";
echo ' if(e.target.className === "magnify"){return;}' . "\n";
echo ' $(".magnify").each(function(){' . "\n";
echo ' if(this.style.zIndex){$(this).click();}' . "\n";
echo ' });' . "\n";
echo ' });' . "\n";
echo '});' . "\n";
echo '</script>' . "\n";

or:


echo '<script type="text/javascript">
jQuery(function($){
$(document).click(function(e){
if(e.target.className === \'magnify\'){return;}
$(\'.magnify\').each(function(){
if(this.style.zIndex){$(this).click();}
});
});
});
</script>';

or even:


?>
<script type="text/javascript">
jQuery(function($){
$(document).click(function(e){
if(e.target.className === 'magnify'){return;}
$('.magnify').each(function(){
if(this.style.zIndex){$(this).click();}
});
});
});
</script>
<?php

What you did however is fine. And there are probably other options for dealing with this sort of situation.