One of the common tasks in the normal development cycle of web pages is to show an image gallery. As with most web sites, an image gallery has a lot of thumbnails and when the user hovers the mouse over an image, we would like to show a big version of the image.
Question: Given a ASP.NET web page with lots of thumbnail images, write jQuery handler to show an enlarged image when a user moves the mouse over an image.
To solve this problem, let’s first define how our web page looks like. Basically, we will have a bunch of thumbnails on the page and an empty div element to contain the full image view of the selected thumbnail. By selected, I mean the image over which the user is hovering.
In addition, we will also define our thumbnail and full image size using CSS classes. These CSS classes will also play an important role in jQuery selectors.
Our aspx page looks like the following so far:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ImageGallery.aspx.cs"
Inherits="jQueryInterviewQuestions.ImageGallery" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Image Gallery</title>
<!-- include jQuery -->
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<!-- define some simple CSS for the thumbnail and full image -->
<style type="text/css">
.thumbnail {position:relative;width:100px;height:75px;}
.image {position:relative;width:400px;height:300px;}
</style>
</head>
<body>
<form id="form1" runat="server">
<!-- the main div containing the image thumbnails -->
<div align="center">
<asp:Image ID="Image1" CssClass="thumbnail" ImageUrl="~/images/1.jpg" runat="server" />
<asp:Image ID="Image2" CssClass="thumbnail" ImageUrl="~/images/2.jpg" runat="server" />
<asp:Image ID="Image3" CssClass="thumbnail" ImageUrl="~/images/3.jpg" runat="server" />
<asp:Image ID="Image4" CssClass="thumbnail" ImageUrl="~/images/4.jpg" runat="server" />
<asp:Image ID="Image5" CssClass="thumbnail" ImageUrl="~/images/5.jpg" runat="server" />
</div>
<!-- this div will contain the full size image -->
<div id="fullImageDiv" >
</div>
</form>
</body>
</html>
Now let’s see how we can achieve this full image effect on hover using jQuery. We will use the CSS selector to find all thumbnails. Now for this collection, we will tap into the hover() effect and change the opacity of all thumbnails to lets say 0.5. Now for more prettier UI, let’s animate and bring back the opacity of the selected thumbnail to 1 in a slow motion. As the final step, let’s set the image from the selected thumbnail in a new image tag in the fullImageDiv. To select the image source for the current selected thumbnail, you can use $(this).attr("src").
When you use jQuery to chane the DOM on events, always make sure to clean up after the event finishes. In our case, we will hook to the mouseleave event and bring the opacity back to 1 for all thumnails and remove the image we added to the fullImageDiv.
The JQuery and the ASP.NET code in its full form looks like this now:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ImageGallery.aspx.cs"
Inherits="jQueryInterviewQuestions.ImageGallery" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Image Gallery</title>
<!-- include jQuery -->
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<!-- this is where all the magic happens -->
<script language="javascript" type="text/javascript">
$(document).ready(function ()
{
// hook to the hover event
$(".thumbnail").hover(
function ()
{
// "dim" the opacity for all thumbnails
$(".thumbnail").css("opacity", "0.5");
// animate the opacity of the selected image
$(this).animate({ opacity: 1.0 }, 200);
// set the selected thumbnail image
// in the fullImageDiv
$("#fullImageDiv").append("<img class='image' src='" +
// get the image source for the selected thumbnail
$(this).attr("src") + "'/>");
},
// in the mouseleave event, cleanup
function ()
{
// reset the thumbnail opacities
$(".thumbnail").css("opacity", "1.0");
// remove the image we just added
$(".image").remove();
}
);
});
</script>
<!-- define some simple CSS for the thumbnail and full image -->
<style type="text/css">
.thumbnail {position:relative;width:100px;height:75px;}
.image {position:relative;width:400px;height:300px;}
</style>
</head>
<body>
<form id="form1" runat="server">
<!-- the main div containing the image thumbnails -->
<div align="center">
<asp:Image ID="Image1" CssClass="thumbnail" ImageUrl="~/images/1.jpg" runat="server" />
<asp:Image ID="Image2" CssClass="thumbnail" ImageUrl="~/images/2.jpg" runat="server" />
<asp:Image ID="Image3" CssClass="thumbnail" ImageUrl="~/images/3.jpg" runat="server" />
<asp:Image ID="Image4" CssClass="thumbnail" ImageUrl="~/images/4.jpg" runat="server" />
<asp:Image ID="Image5" CssClass="thumbnail" ImageUrl="~/images/5.jpg" runat="server" />
</div>
<!-- this div will contain the full size image -->
<div id="fullImageDiv" >
</div>
</form>
</body>
</html>
ReplyDeleteThe blog or and best that is extremely useful to keep I can share the ideas
of the future as this is really what I was looking for, I am very comfortable and pleased to come here. Thank you very much.
tanki online | 2048 game |
tanki online game
I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts. Python Projects for Students Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account. Project Center in Chennai
Delete• I've read all of your information that you shares in your article and I love it. Many thanks for showing this post. I enjoy it.
ReplyDelete* Boy names starting with W
Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. Tableau Data Blending
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteNice post and valuable information thank you.
ReplyDeleteData Analytics Course in Pune
Nice post and valuable information, writing is simply great, thanks for sharing.
ReplyDeleteBusiness Analytics Course in Pune
Business Analytics Training in Pune
Many professional and amateur photographers are learning painful lessons when it comes to slide scanners. IVATION 22MP DIGITAL FILM SCANNER
ReplyDeleteJust saying thanks will not just be sufficient, for the fantastic lucidity in your writing. I will instantly grab your rss feed to stay informed of any updates.
ReplyDeleteData Science Certification in Bangalore
You are in point of fact a just right webmaster. The website loading speed is amazing. It kind of feels that you're doing any distinctive trick. Moreover, The contents are masterpiece. you have done a fantastic activity on this subject!
ReplyDeleteBusiness Analytics Training in Hyderabad | Artificial Intelligence Course in Hyderabad | Business Analytics Course in Hyderabad
Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.
ReplyDeleteData Science Course
I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!
ReplyDeleteData Science Training
I truly like you're composing style, incredible data, thankyou for posting.
ReplyDeletedata science course
Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
ReplyDeletedata science course in hyderabad
data analytics course in hyderabad
business analytics course
Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained!
ReplyDeletedata science course in guwahati
Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!
ReplyDeleteData Science Course in Pune
Data Science Training in Pune
I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
ReplyDeletedata science institute in hyderabad
data science training
data science course
This particular papers fabulous, and My spouse and i enjoy each of the perform that you have placed into this. I’m sure that you will be making a really useful place. I has been additionally pleased. Good perform! this link
ReplyDeleteVery impressive and interesting blog found to be well written in a simple manner that everyone will understand and gain the enough knowledge from your blog being much informative is an added advantage for the users who are going through it. Once again nice blog keep it up.
ReplyDeleteData Science Course in raipur
Very good points you wrote here..Great stuff...I think you've made some truly interesting points.Keep up the good work.
ReplyDeletedata science using python and r programming Hyderabad
Attend The Data Science Courses From ExcelR. Practical Data Science Courses Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Science Courses.
ReplyDeleteData Science Courses
ReplyDeleteThis type of very helpful article. Very interesting to see this article.
Data Science Course In India
ReplyDeleteAdditionally, this is an excellent article which I truly like studying. It's not everyday I have the option to see something similar to this.
Data Science Course Syllabus
I have to search sites with relevant information ,This is a
ReplyDeletewonderful blog,These type of blog keeps the users interest in
the website, i am impressed. thank you.
Data Science Course in Bangalore
This is an excellent post I seen thanks to share it. It is really what I wanted to see hope in future you will continue for sharing such a excellent post.
ReplyDeleteethical hacking course training in Guwahati
It's like you understand the topic well, but forgot to include your readers. Maybe you should think about it from several angles.
ReplyDeleteData Science In Bangalore
I wanted to leave a little comment to support you and wish you the best of luck. We wish you the best of luck in all of your blogging endeavors.
ReplyDeleteData Science Institute in Bangalore
It's really nice and informative, it has all the information and it also has a big impact on new technologies. Thanks for sharing it.
ReplyDeleteData Science Course in Pune
I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
ReplyDeleteartificial intelligence course in pune
This comment has been removed by the author.
ReplyDeleteYou can comment on the blog ordering system. You should discuss, it's splendid. Auditing your blog would increase the number of visitors. I was very happy to find this site. Thank you...
ReplyDeleteData Analytics Course in Bangalore
With so many books and articles appearing to usher in the field of making money online and further confusing the reader on the real way to make money.
ReplyDeleteDigital Marketing Course in Bangalore
You can comment on the blog ordering system. You should discuss, it's splendid. Auditing your blog would increase the number of visitors. I was very happy to find this site. Thank you...
ReplyDeleteBusiness Analytics Course
You actually make it seem like it's really easy with your acting, but I think it's something I think I would never understand. I find that too complicated and extremely broad. I look forward to your next message. I'll try to figure it out!
ReplyDeleteBest Data Science Courses in Bangalore
I am a new user of this site, so here I saw several articles and posts published on this site, I am more interested in some of them, hope you will provide more information on these topics in your next articles.
ReplyDeleteBest Data Science Courses in Bangalore
I am more curious to take an interest in some of them. I hope you will provide more information on these topics in your next articles.
ReplyDeleteData Analytics Course in Bangalore
This is my first time visiting here. I found a lot of funny things on your blog, especially your discussion. From the tons of comments on your posts, I guess I'm not the only one who has all the free time here. Keep up the good work. I was planning to write something like this on my website and you gave me an idea.
ReplyDeleteDigital Marketing Course in Bangalore
Very wonderful informative article. I appreciated looking at your article. Very wonderful reveal. I would like to twit this on my followers. Many thanks!
ReplyDeleteAWS Training in Hyderabad
Very good message. I stumbled across your blog and wanted to say that I really enjoyed reading your articles. Anyway, I will subscribe to your feed and hope you post again soon.
ReplyDeleteBusiness Analytics Course
I was browsing the internet for information and found your blog. I am impressed with the information you have on this blog.
ReplyDeleteBest Data Science Courses in Bangalore
I bookmarked your website because this site contains valuable information. I am very satisfied with the quality and the presentation of the articles. Thank you so much for saving great things. I am very grateful for this site.
ReplyDeleteData Analytics Course in Bangalore
You actually make it seem like it's really easy with your acting, but I think it's something I think I would never understand. I find that too complicated and extremely broad. I look forward to your next message. I'll try to figure it out!
ReplyDeleteDigital Marketing Course in Bangalore
A good blog always contains new and exciting information and as I read it I felt that this blog really has all of these qualities that make a blog.
ReplyDeleteDigital Marketing Course in Bangalore
They are produced by high level developers who will stand out for the creation of their polo dress. You will find Ron Lauren polo shirts in an exclusive range which includes private lessons for men and women.
ReplyDeleteData Analytics Course in Bangalore
Đặt vé tại phòng vé Aivivu, tham khảo
ReplyDeletevé máy bay đi Mỹ tháng nào rẻ nhất
ve may bay my ve vietnam
thông tin chuyến bay từ canada về việt nam
tin tức các chuyến bay từ nhật về việt nam
giá vé máy bay hàn quốc về việt nam
Vé máy bay từ Đài Loan về Việt Nam
danh sách khách sạn cách ly tại tphcm
ve may bay chuyen gia sang Viet Nam
It's like you've got the point right, but forgot to include your readers. Maybe you should think about it from different angles.
ReplyDeleteDigital Marketing Course in Bangalore
It's like you've got the point right, but forgot to include your readers. Maybe you should think about it from different angles.
ReplyDeleteBusiness Analytics Course
It's like you've got the point right, but forgot to include your readers. Maybe you should think about it from different angles.
ReplyDeleteBest Data Science Courses in Bangalore
You have completed certain reliable points there. I did some research on the subject and found that almost everyone will agree with your blog.
ReplyDeleteData Analytics Course in Bangalore
You have completed certain reliable points there. I did some research on the subject and found that almost everyone will agree with your blog.
ReplyDeleteDigital Marketing Course in Bangalore
You have completed certain reliable points there. I did some research on the subject and found that almost everyone will agree with your blog.
ReplyDeleteBusiness Analytics Course
I am a new user of this site, so here I saw several articles and posts published on this site, I am more interested in some of them, will provide more information on these topics in future articles.
ReplyDeleteData Analytics Course in Bangalore
The blog is good enough, keep up writing such type of posts.
ReplyDeletebranding firms SF
Very informative message! There is so much information here that can help any business start a successful social media campaign!
ReplyDeleteData Analytics Course in Nashik
It's like you've got the point right, but forgot to include your readers. Maybe you should think about it from different angles.
ReplyDeleteData Science Training in Erode
It's like you've got the point right, but forgot to include your readers. Maybe you should think about it from different angles.
ReplyDeleteData Analytics Course in Bangalore
ReplyDeleteNice information. I’ve bookmarked your site, and I’m adding your RSS feeds to my Google account to get updates instantly. Em Editor Crack
Very informative message! There is so much information here that can help any business start a successful social media campaign!
ReplyDeleteBusiness Analytics Course in Erode
Very informative message! There is so much information here that can help any business start a successful social media campaign!
ReplyDeleteData Scientist Course in Gorakhpur
It's like you've got the point right, but forgot to include your readers. Maybe you should think about it from different angles.
ReplyDeleteData Analytics Course in Nashik
Very informative message! There is so much information here that can help any business start a successful social media campaign!
ReplyDeleteData Science Course in Kolkata
Wonderful blog post. This is absolute magic from you! I have never seen a more wonderful post than this one. You've really made my day today with this. I hope you keep this up! data science course in Patna
ReplyDeleteI have read your excellent post. This is a great job. I have enjoyed reading your post first time. I want to say thanks for this post. Thank you... data science course in Vadodara
ReplyDeleteI really loved reading your blog. It was very well authored and easy to understand. Unlike other blogs I have read which are really not that good.Thanks alot! data science course in Kolkata
ReplyDeletewow, great, I was wondering how to cure acne naturally. and found your site by google, learned a lot, now i’m a bit clear. I’ve bookmark your site and also add rss. keep us updated. data science course in Lucknow
ReplyDeleteGet the best Data Science online course at the comfort of your home. Flexible timings, Best industry trainers, and meticulously crafted curriculum. Avail now!!!!business analytics course in varanasi
ReplyDelete360DigiTMG offers the best Data Analytics courses in the market with placement assistance. Enroll today and fast forward your career.
ReplyDeleteGet dual certification from IBM and UTM Malaysia with the 360DigiTMG Data Science Certification program.business analytics course in jalandhar
ReplyDelete