05.05.2010 Updated code to adapt the position of the image depending on height and width (and scroll) of the page relative to the image size.
I got this request from Mark:
Hi Alexander,
I did not include the column as a picture because they are to big to display in the list view. For this reason I setup a list with a hyperlink to the image.
Besides it would look terrible having a list that displays all these large pictures in a list view. For that reason, I have been trying to find some way to display an image from a list when a user hovers over a row or column.

Add this code to a CEWP below the list view, and it will add a hover function to all hyperlinks including an image (add more types to the array “arrOfImageTypes”):
<script type="text/javascript" src="../../Javascript/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
function imagePreview(){
arrOfImageTypes = ['jpg','jpeg','gif','png'];
$("table.ms-listviewtable td.ms-vb2 a").hover(function(e){
var href = this.href;
var img = href.substring(href.lastIndexOf('.')+1).toLowerCase();
if(href.indexOf('http')==0 && $.inArray(img,arrOfImageTypes)>-1){
$("body").append("<img id='preview' src='"+ this.href +"' alt='Image preview' />");
}
var obj = $("#preview");
var offset = $(this).offset();
var winHeight = $(window).height();
var winWidth = $(window).width();
var scrollLeft = $(window).scrollLeft();
var scrollTop = $(window).scrollTop();
var objHeight = obj.outerHeight();
var objWidth = obj.width()+15;
if(((winWidth+scrollLeft)-offset.left)<objWidth){
offset.left=((winWidth+scrollLeft)-objWidth);
}
var maxHeight = (winHeight+scrollTop)-offset.top;
if(objHeight>maxHeight){
if(offset.top-scrollTop>objHeight){
offset.top=offset.top-objHeight-20;
}
height = (objHeight<winHeight)?objHeight:winHeight;
}
obj.css({"position":"absolute","top":(offset.top+20)+"px","left":offset.left+20})
.fadeIn("fast");
},
function(){
$("#preview").remove();
});
};
// Call the script on page load
$(document).ready(function(){
imagePreview();
});
</script>
Regards
Alexander
April 29, 2010 at 9:53 pm |
I tried this 2 different ways:
1. added a list to a web part then added a CEWP below the list web part copied the script above and pasted it. Nothing happened.
Then I thought okay it may not work within a web part so
2. went to the actual list and edited the page added the CEWP below the actual list and pasted the code. Nothing happened.
What am I missing?
April 29, 2010 at 10:12 pm
You must update the path to your copy of jquery.
How to troubleshoot when the scripts does not work
Alexander
April 30, 2010 at 8:35 pm
Just color me stupid, thanks!
April 30, 2010 at 7:07 pm |
Hi Alexander:
This is a wonderful solution!!
Do you have any way that it can work with a .pdf extension?
Thanks-
Charlie Epes
Buffalo, NY
May 6, 2010 at 3:34 pm |
Hi Alexander:
sorry to push but…
Do you have any way that it can work with a .pdf extension?
Charlie
May 6, 2010 at 3:42 pm
Hi,
I do not think this is possible with the logic from the above post. It would be possible to open a new window with the pdf embedded, but this would not be very smooth.
Alexander
May 11, 2010 at 10:45 pm |
Hi Alexander,
Great solution. I am using this on the default.aspx for my SharePoint home page on a image. I can see the image popup but noly when entering from the top-left or bottom-right corner. It is not as smooth as in the list. Any reason??
May 11, 2010 at 11:27 pm
Hi,
I did a quick test and could not recreate this behavior. Do you have any other custom code in the page?
Alexander
May 11, 2010 at 11:32 pm |
Nope…..I don’t have any custom code other then this. I have a small image 188×180 px for which there is a link which takes to a bigger image 600x500px. This doesn’t preview properly some how……I can send you the code…….
May 11, 2010 at 11:40 pm
Email me the code and a screenshot of the webpart with the “problem”.
I will take a look at it tomorrow (I’m om GMT+1 and it is bedtime…).
Alexander
July 21, 2010 at 3:36 pm |
I have a tasking to hoover over an image and get the information in the comment field of my sharepoint list to display. So instead of hoovering over a link to get a picture; I’d need to hoover over a icon to preview a user comment. Will this solution work for the later?
July 21, 2010 at 9:42 pm
Hi,
No, it will not. Take a look at this one instead: Preview metadata in list view on mouseover
Alexander
July 26, 2010 at 6:39 pm |
I have used your code to display a list linking to a library of images. When hovering over the hyperlink “show image”, which is the descriptive text for the pic, it displays two (2) images. One image is placed where a normal tooltip image would be – directly to the right of the cursor. But another second image appears to the far left of the screen slightly lower than the original popup image.
I downloaded jQuery as in your example. Created a library to hold the files. Added your code to a .txt file and uploaded it to a page. Dropped a CEWP on a page and linked to the .txt file and then hid the CEWP. Dropped the custom list with the image hyperlinks on the same page.
Any ideas?
July 27, 2010 at 9:17 am
Hi,
What browser do you use?
Alexander
July 27, 2010 at 3:32 pm
I am using Firefox.
July 27, 2010 at 3:33 pm
Sorry, I am using IE7 for SharePoint testing
July 31, 2010 at 8:19 pm
Please send some screenshots of the page and the code. You find the address here http://sharepointjavascript.wordpress.com/about/
Alexander
December 9, 2010 at 11:25 pm |
Wow. This is something I’ve been looking for…sort of. Would this code adapt to show a preview of wiki pages (from the wiki pages library) when hovering over the link on the sharepoint wiki homepage? If not, do you have such a code?
January 18, 2011 at 8:40 pm
Hi,
Sorry for the late reply, but there are so many questions and so little time…
The answer is no I’m afraid. This solution is for images only.
Alexander
January 4, 2011 at 5:15 pm |
Hi Alexander, I was woundering whether the code you’ve used here is amendable in terms of adding additional file types to be displayed. I’m relativly new to sharepoint (just over a years experience) and i’m trying to make a few amendments to my site. Alot of the users use a wide range of different image types including Gif. I used your code on my sandbox server and it works beautifully, so I’m hoping i can extend the range.
Cheers in advance
January 18, 2011 at 8:42 pm
Hi,
Look at “arrOfImageTypes” in line 05.
Alexander
January 19, 2011 at 12:48 pm |
Hi Alexander,
is there a way to check if the image exist? I didn’t find a solution to check if e.g. the jpg exists and only when the Image exist show the preview!
I tried to change your code an build the url to the image (not from the hyperlink itself!), but i can not check if the “generated” url is pointing to a existing image…
January 19, 2011 at 7:21 pm |
Hello Alexander,
I have a folder called images, and each jpg is labeled by username. I also have a directory that lists each person, which is provided from the directory. Do you know how if I hover over the link to each persons profile page, how can I get the popup to display the image for that person?
Thanks,
Aaron
January 20, 2011 at 4:34 pm |
Hi Alexander,
I am new to sharepoint. Is there any way to use this logic on a wiki page rather than a list to create a hover option for links to images?
Thank you!
March 10, 2011 at 10:39 pm |
Hi Alexander
I am creating a new web site for myself, that in itself is a challenge!
However I came across your site, and I wondered if maybe you can help me?
I have some type on my site that I need to display images as the user hovers over them.
Small images, and as the user moves away the image disappears!
Will your script do this? And if so, how exactly do I get it to do this?
I am using dreamweaver CS4
March 24, 2011 at 8:33 pm |
Hello Alexander,
This was a great script. I am using wordpress, and with a little modification I works there as well. I am using the members list plugin and I set it up so that I can mouse over a person’s name and see their photo.
Thanks,
Aaron
May 22, 2012 at 2:33 pm |
scripts | shop scripts | web scripts | website tools | tutorials | design | themes | php | html | html5 | clone…
[...]Image preview on hover over a hyperlink field « SharePoint JavaScripts[...]…