Thumbnail image should be swapped with the current image on click - javascript

I have a codepen link https://codepen.io/robgolbeck/pen/bVGmop which is quite similar to what I want. The only thing I want is that on click of thumbs image the large image should be replaced with the clicked thumbs image and it should not be present in the thumbs list.In total only 5 images should be present in rotation whereas here there are 6(5+ 1repeated in thumbs). I have tried to tweak the JS but it doesn't work as expected. Can anyone please help. Stuck with this problem since morning. Thanks in advance.
JS
$('#thumbs img').click(function(){
$('#largeImage').attr('src',$(this).attr('src').replace('thumb','large'));
$(this).attr('src',$('#largeImage').attr('src').replace('large','thumb'));
});

in the click handler event of #thumbs img, you can first show all the images and then hide current image. to start you can trigger click on first image.
$('#thumbs img').click(function(){
$('#largeImage').attr('src',$(this).attr('src').replace('thumb','large'));
$('#description').html($(this).attr('alt'));
$('#thumbs img').show();
$(this).hide();
});
$('#thumbs img').first().trigger('click');

You can simply use jQuerys show() and hide() methods. First show all child elements of #thumbs and then hide the currently clicked element with $(this).hide();.
To initially hide the first image, you can use $('#thumbs').children().first().hide();
$('#thumbs').children().first().hide();
$('#thumbs img').click(function() {
$('#largeImage').attr('src', $(this).attr('src').replace('thumb', 'large'));
$('#description').html($(this).attr('alt'));
$('#thumbs').children().show();
$(this).hide();
});
#thumbs {
padding-top: 10px;
overflow: hidden;
}
#thumbs img,
#largeImage {
border: 1px solid gray;
padding: 4px;
background-color: white;
cursor: pointer;
}
#thumbs img {
float: left;
margin-right: 6px;
}
#description {
background: black;
color: white;
position: absolute;
bottom: 0;
padding: 10px 20px;
width: 525px;
margin: 5px;
}
#panel {
position: relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="gallery">
<div id="panel">
<img id="largeImage" src="http://robgolbeck.com/demos/image-swap/image_01_large.jpg" />
</div>
<div id="thumbs">
<img src="http://robgolbeck.com/demos/image-swap/image_01_thumb.jpg" alt="1st image description" />
<img src="http://robgolbeck.com/demos/image-swap/image_02_thumb.jpg" alt="2nd image description" />
<img src="http://robgolbeck.com/demos/image-swap/image_03_thumb.jpg" alt="3rd image description" />
<img src="http://robgolbeck.com/demos/image-swap/image_04_thumb.jpg" alt="4th image description" />
<img src="http://robgolbeck.com/demos/image-swap/image_05_thumb.jpg" alt="5th image description" />
</div>
</div>

I think attr method of jQuery is causing issue, try using prop method as shown in below snippet :
$('#thumbs img').click(function(){
$('#largeImage').prop('src',$(this).prop('src').replace('thumb','large'));;
$('#description').html($(this).prop('alt'));
});

$('#thumbs img').click(function(){
$("#thumbs img").each(function(){$(this).show();});
$('#largeImage').attr('src',$(this).attr('src').replace('thumb','large'));
$('#description').html($(this).attr('alt'));
$(this).hide();
});

Related

jQuery link with roll down image

I'm trying to put an .animate in a function to show more of an image when you hover over it. So far I only managed to roll down the entire div element, somehow I can't figure out how to talk to each individual image. This is what I have.
HTML:
<div class="social">
<img src="xxx" alt="xxx">
<img src="xxx" alt="xxx">
<img src="xxx" alt="xxx">
</div>
CSS:
.social{
position: fixed;
top: -91px;
right: 10%;
}
.social img{
width: 25px;
margin: 0 5px 0 0;
}
javaScript/jQuery (works if I change $('.social a img') to $('.social')):
$(document).ready(function () {
$('.social a img').hover(function ( ){
$(this).animate({top:'0px'});
});
});

How can I make png image act as a button?

I am using Dreamweaver for the first time to code. I am intermediate in HTML.
I've been trying to use a png file as a button. I've found some sources stating that a ...
<button src=Home_button> </button>
... will work, but I have tried it, and to no avail, it does not work. Any suggestions?
NOTE:
I am also using a CSS to build this Very basic website.
Just add background-image to your button.
<button id="home_button">click me</button>
and then add:
#home_button {
background-image: url(...);
}
You may need to add further styling to it of course, such as widths and other background properties to get it just right. But this is how you add a background image to a button.
Here's a working demo as well:
#home_button {
width: 150px;
height: 150px;
background-image: url('http://i.stack.imgur.com/sfed8.png');
background-size: cover;
background-color: #eee;
}
<button id="home_button"></button>
You can add an img tag inside your button tag.
<button id="bar" type="submit"><img src="http://cdn.sstatic.net/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a" /></button>
There are many ways to create something that looks like an image and behaves like a button.
Here-below are code examples demonstrating 5 options worth considering...
Option 1 :
You could put an <img> element inside a <button> element :
document.getElementById("myButton").addEventListener("click", function() {
alert('Button clicked');
});
button, img {
padding: 0;
font-size: 0;
border: none;
}
<button id="myButton">
<img src="http://www.gravatar.com/avatar/bf4cc94221382810233575862875e687?s=150" alt="">
</button>
(see also this Fiddle)
Option 2 :
You could use an <a>-tag instead :
document.getElementById("myButton").addEventListener("click", function() {
alert('Button clicked');
});
a {
border: none;
display: inline-block;
font-size: 0;
}
<a id="myButton" href="#">
<img src="http://www.gravatar.com/avatar/bf4cc94221382810233575862875e687?s=150" alt="">
</a>
(see also this Fiddle)
Option 3 :
You could just attach your click handler directly to your image :
document.getElementById("myButton").addEventListener("click", function() {
alert('Button clicked');
});
<img id="myButton" src="http://www.gravatar.com/avatar/bf4cc94221382810233575862875e687?s=150" alt="">
(see also this Fiddle)
Option 4 :
You could set your image as a background-image of a <button>-tag or other tag that represents a button.
document.getElementById("myButton").addEventListener("click", function() {
alert('Button clicked');
});
input[type=submit] {
background: url("http://www.gravatar.com/avatar/bf4cc94221382810233575862875e687?s=150");
width: 150px;
height: 150px;
border: none;
}
<input type="submit" id="myButton">
(see also this Fiddle)
Option 5 :
You could set your image as a background-image of an <a>-tag, a <div>-tag or another tag that doesn't represent a button :
document.getElementById("myButton").addEventListener("click", function() {
alert('Button clicked');
});
div {
background: url("http://www.gravatar.com/avatar/bf4cc94221382810233575862875e687?s=150");
width: 150px;
height: 150px;
border: none;
}
<div id="myButton"></div>
(see also this Fiddle)
You could do <img src="#" alt="" /> to achieve what you want.
There are several ways to use an image as a button..
<a href="your_link.html">
<img id="mypic' src="path_to_your.png" alt="MyImage" width="200" height="300" />
</a>
css..
#mypic {
background: #390239;
color:#7e4a00;
border:#7e4a00 2px solid;
border-color:;
outline:none;
padding: 6px;
border-radius:5px;
text-align: center;
}
Chris also has a good answer... it depends on what looks good on the page, as well as your coding knowledge.
Here's what you can do...
HTML
<button>Click Me</button>
CSS
button {
display: inline-block;
height: 50px // height of your button image
text-indent: -9999px // hide if you have text inside <button>
width: 100px // width of your button image
}
Hope this will help :)

how I can give link to slide to redirect to other page

I am creating a slide show in with help of Jquery as below.I am able to get the slide show .But I am not able to give the link to each slide using href
Plseae help me .
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<style>
.fade {
position: relative;
width: 650px;
height: 373px;
}
.fade img {
position: absolute;
left: 0;
top: 0;
width: 650px;
height: 373px;
}
</style>
<script>
$(function () {
$('.fade img:gt(0)').hide();
setInterval(function () {
$('.fade :first-child').fadeOut(3000)
.next('img').fadeIn(3000).end()
.appendTo('.fade');
}, 4000);
});
</script>
<div class="fade" style="width: 603px; height: 373px; z-index: 1; border-radius: 10px 10px 10px 10px; left: 295px; top: 224px; position: absolute">
<img src="images/SAM_0043.JPG" href="www.xyz.com" >
<img src="images/SAM_0047.JPG" href="www.xyz.com" >
<img src="images/SAM_0044.JPG" href="www.xyz.com" >
</div>
</body>
Try
<img src="images/SAM_0043.JPG" >
img tag has no href attribute.
wrap img tag with a tag n give href
Or with jQuery using your current code wrap a tag around img
fiddle Demo
$('div.fade img').wrap(function () {
return '';
});
embed the img tag into the anchor tag, and provide the href="" to the anchor tag. Your work is done.
href is not an img tag attribute so better to change the html to
<img src="" />
and if you dont want to change your html you can use code below to redirect to links on click on images
$(".fade img").bind('click',function(){
window.location = $(this).attr("href");
});

Changing image within div when clicked

Spent a couple of days on this now and need some guidance. I have a toggle with an image within the div. Basically when the div is clicked the image within it should change to a different image.
Here is a link to the jsfiddle, so you can see the image at the top which should change once the whole toggle area is clicked.
http://jsfiddle.net/VLe8g/
Also here is the code
HTML
<div class="toggle-wrap">
<div class="trigger">
<div class="one-third"><img src="http://placehold.it/200x200" /></div>
<div class="two-thirds column-last">This is where the heading goes <br />
This is where the description goes<br />
Toggle Open</div>
</div>
<div class="toggle-container">
<div class="one-third">First column This is where the heading goes This is where the heading goes</div>
<div class="one-third">Second column This is where the heading goes This is where the heading goes</div>
<div class="one-third column-last">Third column This is where the heading goes This is where the heading goes</div>
</div>
</div>
CSS
.toggle-wrap {
float: left;
width: 100%;
margin-bottom: 5px;
}
.trigger {}
.trigger a {
display: block;
padding: 10px;
padding-left: 15px;
text-decoration: none;
font-weight: bold;
color: #1D1D1B;
-webkit-transition-duration: 0s;
-moz-transition-duration: 0s;
-o-transition-duration: 0s;
background: url(tobeadded) no-repeat right 15px #F3F3F3;
}
.trigger.active a {
background: url(tobeadded) no-repeat right -20px #F3F3F3;
}
.toggle-container {
overflow: hidden;
float: left;
padding: 15px 15px 0 15px;
}
.one-third, .two-thirds {
display: inline;
float: left;
margin-right: 4%;
}
.one-third {
width: 30.66%;
}
.two-thirds {
width: 64%;
}
JAVASCRIPT
$(".toggle-container").hide();
$(".trigger").toggle(function(){
$(this).addClass("active");
}, function () {
$(this).removeClass("active");
});
$(".trigger").click(function(){
$(this).next(".toggle-container").slideToggle();
});
Hope you guys can help me out.
Thanks.
Demo here
Try this:
$(".toggle-container").hide();
$(".trigger").toggle(function () {
$(this).addClass("active");
$(".trigger").find('img').prop('src', 'http://jsfiddle.net/favicon.png');
}, function () {
$(this).removeClass("active");
$(".trigger").find('img').prop('src', 'http://placehold.it/200x200');
});
$(".trigger").click(function () {
$(this).next(".toggle-container").slideToggle();
});
$(this).find("img").attr("src", "http://placehold.it/400x400");
will change the image from a 200x200 to a 400x400. You can add a class to both of them (i.e. class=small, class=big) which jQuery can use to identify which one is currently being displayed and toggle between them.
assign an id to image tag like i did
<img id="test" src="http://placehold.it/200x200" />
and use the following code:
$(".toggle-container").hide();
$(".trigger").toggle(function(){
$(this).addClass("active");
$('#test').attr('src','http://placehold.it/200x200');
}, function () {
$(this).removeClass("active");
$('#test').attr('src','https://fbcdn-dragon-a.akamaihd.net/hphotos-ak-ash3/s200x200/851558_201013993381868_2094147002_n.png');
});
$(".trigger").click(function(){
$(this).next(".toggle-container").slideToggle();
});
Add the id attribute to your img, and then change the src attribute every time the toggle handler is executed.
The img definition should something like this:
<img id="myImage" src="http://placehold.it/200x200" />
and your toggle handler should look like this:
$(".trigger").toggle(function(){
$(this).addClass("active");
$("#myImage").attr('src', 'http://placehold.it/some_other_image');
}, function () {
$(this).removeClass("active");
$("#myImage").attr('src', 'http://placehold.it/200x200');
});

Jquery enlarge image from thumbnail in jQuery error

I'm trying to replicate an example of an jquery plugin that enlarges image from thumbnail at my desktop and the problem is , the image doesn't enlarge from the thumbnail whenever I click on it
Here is the original source of the jquery demo that enlarges image from thumbnail that i'm trying to replicate
How to enlarge image from thumbnail in jQuery?
This is his working demo http://jsbin.com/egevij/3/edit
The problem is in my HTML .Can someone please point where I'm going wrong?
This is my HTML
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<link rel="stylesheet" href="css/forms.css">
<meta charset=utf-8 />
<title>Demo by roXon</title>
</head>
<body>
<div id="jQ_popup_window">
<div id="jQ_popup" class="shadow radius">
<div id="jQ_popup_close"></div>
<script type = "text/javascript" src ="trouble.js"></script>
</div>
</div>
<img src="http://placehold.it/250x150/cf5" data-full="http://placehold.it/860x590/cf5" alt="" />
<img src="http://placehold.it/250x150/fof" data-full="http://placehold.it/860x590/fof" alt="" />
</body>
</html>
forms.css CSS
CSS:
/* === POPUP WINDOW === */
#jQ_popup_window{
background: rgba(0,0,0,0.6);
left: 0;
margin-left: -9000px;
position: absolute;
top: 0;
width: 100%;
z-index:999999;
}
#jQ_popup {
background: #000;
border: 1px solid #BDB9B8;
margin: 30px auto;
padding: 25px;
position: relative;
width: 600px; /* SET HERE DESIRED W .*/
}
#jQ_popup_close {
background:#fff;
cursor: pointer;
height: 28px;
width: 28px;
position: absolute;
z-index:999999;
right: 10px;
top: 10px;
-webkit-border-radius:30px;
border-radius:30px;
border:2px solid #fff;
border-color: rgba(255,255,255,0.2);
}
#jQ_popup_close:hover{
background:#f00;
}
/* #POPUP WINDOW */
jQuery:
// POPUP WINDOW:
var scrT = $(window).scrollTop();
$(window).scroll(function(){
scrT = $(window).scrollTop();
});
// GET and use WINDOW HEIGHT //
$.getDocHeight = function(){
var D = document;
return Math.max(Math.max(D.body.scrollHeight, D.documentElement.scrollHeight), Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), Math.max(D.body.clientHeight, D.documentElement.clientHeight));
};
// POPUP WINDOW (lightbox for video and other)
// GET WINDOW SCROLLtop OFFSET
$('[data-full]').on('click', function(e){
e.preventDefault();
$('#jQ_popup').css({
top: scrT+15
}).find('img').remove();
$('#jQ_popup_window').height($.getDocHeight).fadeTo(0,0).css({
marginLeft:0
}).fadeTo(600,1);
var imgToLoad = $(this).data('full');
$('<img>', {src:imgToLoad, width:'100%'}).appendTo('#jQ_popup');
});
// close popup
$('#jQ_popup_close, #jQ_popup_window').on('click', function(){
$('#jQ_popup_window').fadeTo(600,0,function(){
$(this).hide();
});
});
$('#jQ_popup').on('click', function(ev){
ev.stopPropagation();
});
// end POPUP WINDOW
The solution to your problem is simply moving the import of the JavaScript file. It should be placed at the end after your two <img> tags.
<img src="http://placehold.it/250x150/cf5" data-full="http://placehold.it/860x590/cf5" alt="" />
<img src="http://placehold.it/250x150/fof" data-full="http://placehold.it/860x590/fof" alt="" />
<script type = "text/javascript" src ="trouble.js"></script>
It's standard practice to load your javascript files either last in the head or last in the body. Putting script tags inside of other html tags such as <div> as you have done is not normal but I've never seen it have ill effects like this before.

Categories

Resources