Image change on hover from a stitched image - javascript

I have a image that is created to have 4 images in one See this image
Now, what I am trying to do is get the css to load just the first square at the top. Then when a user hovers over that image it will switch between all four images.
So it will display black on load, then when a user hovers over it, the image changes to red, then blue, then green, then back to black. It then repeats over and over until the mouse is moved off the image area.
I know that I can do this by converting the png to a gif but the image is generated outside of my control so this method is needed.
If anyone can help I will be forever grateful.
Cheers.

You should use CSS sprites and to make the change happend when over use setInterval function to change position ( here the height of each block of your image mesures 300px , so we incerement by +300 ) every defined interval time ,
Below snippet I've used .hover() jquery function to set and clear annimation .
var interval;
$(function(){
$("#image").hover(
function() {
var bottom = 0;
$this = $(this);
interval = setInterval(function() {
bottom >= 900 ? bottom = 0 : bottom+=300;
$this.css({'background-position' : '0px -'+bottom+'px'});
} , 1000)
},
function(){
$this.css({'background-position' : '0 0'})
clearInterval(interval);
}
);
});
#image {
width:150px;
height:150px;
background: url('https://i.stack.imgur.com/h8h14.png') 0 0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="image" ><div>

Related

combine two image and change one of this on click

I have to combine two images as in the diagram (under this message). The green image must be on the red line of another image, the problem is that by reducing the window as soon as the image 1 is resized responsively, the green image moves and no longer remains glued with the other.
I reserve a way to do this (HTML, CSS or jQuery), the two images must behave as if they were one, responsively. Also when I click on the small image this must be changed with another image of the same size but different.
Thanks to everyone who will offer me help.
See the code snippet below (also found here - it doesn't show very well on SO as you can't resize the output window). It's not 100% clear to me what you want to do, but I think this accomplishes it. If not, let me know and I'll amend the answer.
// This position is relative to the original size of the first image.
// i.e. when the window is exactly as large as image 1, image 2 will
// be displayed at (100, 200)
var pos = [100, 200];
// Reposition img2 as the window is resized
function reposition () {
img2.style.left = (pos[0] / img1.naturalWidth * img1.clientWidth) + "px";
img2.style.top = (pos[1] / img1.naturalHeight * img1.clientHeight) + "px";
// The position is re-calculated based on the scaling of image 1
// (its current width / its original width)
// This is multiplied by pos to ensure that image 2 is always in the
// same position proportional to the size of image 1
}
// Run reposition on window changes
window.onresize = function () {
reposition();
}
window.onload = function () {
reposition();
}
// Change the image on click
img2.onclick = function () {
img2.src = "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fc/Unterf%C3%BChrung_B22_M%C3%BCnchner_Ring_3150017.jpg/320px-Unterf%C3%BChrung_B22_M%C3%BCnchner_Ring_3150017.jpg"
}
/* Image 1 will fill the window */
#img1 {
width: 50vw;
}
/* Image 2 has absolute position, and is 1/5 the size of image 1 */
#img2 {
position: absolute;
width: 15vw;
/* Obviously this sizing can be changed */
}
body {
margin: 0;
}
/*
Because both images scale with the window, they will keep
the same size proportional to each other. This could also be done in Javascript if needed.
*/
<!-- Example images -->
<img id="img1" src="https://cdn.pixabay.com/photo/2016/06/18/17/42/image-1465348_960_720.jpg" />
<img id="img2" src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f2/Tunnel_Eierberge_Regional_Express_3300341.jpg/320px-Tunnel_Eierberge_Regional_Express_3300341.jpg" />

Animated GIF background only showing once

So on click of these polaroid like images, I replace them with <div class="poof"></div> using jQuery's .replaceWith(). The reason I do this is to display a poof animated gif animation that I style to that class with js.
However, on click of the .polaroid, the polaroid images, the .poof div replaces that html every time, but its background never shows.
Try it for yourself, here:
Click an image, it is replaced by the .poof div, but the poof animation doesn't show.
I would greatly appreciate it someone could help me figure out why this is and how to get the animation to show each time.
Here is my javascript:
$(function(){
var time=1;
$('.polaroid').click(function(){
$(this).replaceWith('<div class="poof"></div>');
$('.poof').css('height', $(this).height()).css('width', $(this).width()).css('background', 'transparent url(poof.gif?t='+time+') center no-repeat');
time++;
});
});
Here is .poof's CSS:
.poof{
height:32px;
width:32px;
}
You are getting the height and width of an object that was remove / replaced in the DOM
Change to this:
var heights = 32;
var widths = 32;
$('.polaroid').click(function(){
heights = $(this).height();
widths = $(this).width();
$('.poof').css('height', heights).css('width', widths).css('background', 'transparent url(http://i.imgur.com/FsVe2LC.gif?t='+time+') center no-repeat');
time++;
setTimeout(function(){
$('.poof').remove();
}, 1500);
});

Enlarge image on hover and add spotlight text

I am having difficulty finding a tutorial on enlarging an image on mouse hover, then shading the enlarged image dark so I can put some text over it.
This example does almost everything except make the image larger:
http://jsfiddle.net/balupton/FZG3v/
CSS:
.ih { position:relative; }
.ih, .ih-image, .ih-info { display:block;width:330px;height:220px; }
.ih-image, .ih-info { z-index:500;position:absolute;top:0;left:0;right:0;bottom:0; }
.ih-info { z-index:501;background:black;color:white; }
HTML:
<img class="ih-image" src="http://designsnack.com/screenshots/image.php width=330&height=220&cropratio=330:220&image=/screenshots/8264948088.jpg"/>
<div class="ih-info">
Your overlay content
</div>
</div>
JS:
$(function(){
$('.ih').hover(
// Over
function(){
var $ih = $(this);
$ih.find('.ih-info').stop(true,true).animate({opacity:0.8}, 400);
},
// Out
function(){
var $ih = $(this);
$ih.find('.ih-info').stop(true,true).animate({opacity:0}, 400);
}
).find('.ih-info').css('opacity',0);
});
It's my first question, apologies if I am not clear or am missing some information.
Many thanks for any help!
EDIT: Thanks ghost! My end goal is to enlarge the image from the center as ultimately there will be 4 images in a 4X2 configuration.
You can do this many ways using jQuery, one of them is to use animate() method to smoothly change image size: http://jsfiddle.net/FZG3v/44/

Background fade in on click

I am trying get this background to fade in on click. I found one tutorial that was helpful, and I ended up created the code so it has two images, and they fade in and out on click to bring up the picture.
Here's the work: http://www.mccraymusic.com/bgchangetest.html
Only a couple of issues though:
How do I make this work without the images getting selected at random? I'd like it to just switch from the plain black image to the image with the drum set. (And cross-fade to if possible, but not necessary)
How do I center the image on the page, so the image of the drums are centered?
I'm guessing this is what you're after:
$(function() {
var images = ["black.jpg","bg.jpg"];
$('<img>').attr({'src':'http://www.mccraymusic.com/assets/images/'+images[0],'id':'bg','alt':''}).appendTo('#bg-wrapper').parent().fadeIn(0);
$('.entersite').click(function(e) {
e.preventDefault();
var image = images[1];
$('#bg').parent().fadeOut(200, function() {
$('#bg').attr('src', 'http://www.mccraymusic.com/assets/images/'+image);
$(this).fadeIn(1000);
});
$(this).fadeOut(1000, function() {
$(this).remove();
});
});
});​
DEMONSTRATION
Also added :
display: block;
margin-left: auto;
margin-right: auto;
to your #bg element to center the image.
Alright, assuming you use JQuery
You have #backgroundid and #imageid
Begin by setting
$('#backgroundid').css('opacity',1);
$('#imageid').css('opacity',0); // setting opacity (transparency) to 0, invisible
Now you have #buttonid.
Set up a jquery event so that when it's clicked, you fade out the background, and fade in the image using JQuery's animate.
$('#buttonid').click(function() {
$('#backgroundid').animate(function() {
opacity : 0 // fade it to 0 opacity, invisible
}, 1000); // animation will take 1000ms, 1second
$('#imageid').animate(function() {
opacity : 1 // fade it to full opacity, solid
}, 1000);
});
Now about that image centering.
You can either let css manage it with
body { /* Body or #imageid parent */
text-align : center;
}
#imageid {
margin: 0px auto;
}
Or you can stick to a JQuery solution, using absolute/fixed positioning.
First, use some css to fix the position of your image
#imageid {
position: absolute; // or fixed, if you want
}
Now use JQuery to reposition it
function positionImage() {
var imagewidth = $('#imageid').width();
var imageheight = $('#imageid').height();
$('#imageid').css('left', ($(window).width() - imagewidth) / 2);
$('#imageid').css('top', ($(window).height() - imageheight) / 2);
}
$(document).ready(positionImage); // bind the ready event to reposition
$(window).resize(positionImage); // on window resize, reposition image too
if you keep a div element with height and width as 100% and bgcolor as black. And then change the opacity of the div as desired to get the fade in/out effect, that should generate the same effect. I guess..
You are better off using any available jQuery plugin as they would have optimized and fixed bugs for multiple browsers.
Try lightBoxMe plugin
http://buckwilson.me/lightboxme/
This is the simplest plugin available!

Why the box disappear immediately?

I want the mouseover on the coverImg then show the coverInfo
the coverInfo show the title and the description of the image
then the coverInfo do show
but I want the coverInfo stay and clickable when mouserover on itself
but it disappear immediately.
So what's the point I have missed?
The HTML
<div class="workshop_img">
<div class="coverInfo"></div>
<a href="#">
<span class="coverImg" style="background-image:url('images/work/show1.jpg')" title="Chictopia "></span>
</a>
The CSS:
.coverInfo {
position:absolute;
width: 200px;
height:200px;
background:rgba(0,0,0,0.5);
top:30%;
left:30%;
display:none;
}
see the jQuery code
$(function() {
$(".coverImg").each(function() {
//make the background image move a little pixels
$(this).css({
'backgroundPosition' : "-40px 0"
}).mouseover(function() {
$(this).stop().animate({
'backgroundPosition' : " -20px -60px "
}, {
duration : 90
});
//shwo the info box
var content = $(this).attr("title");
$("<div class='coverInfo'></div>").text(content).prependTo($(this).parent()).fadeIn("fast");
}).mouseout(function() {
$(this).stop().animate({
'backgroundPosition' : "-40px 0"
}, {
duration : 200,
});
$(this).parent().find(".coverInfo").stop().fadeOut("fast");
})
})
});
</div>
EDIT:
I have searched a lot and find something similar, I took them and the answer given below together to solve my problem, here is the code:
$(function() {
$(".coverImg").css({
'backgroundPosition' : "-40px 0"
}).mouseenter(function() {
var box = $(this).parents(".workshop_img").find(".coverInfo");
var content = $(this).attr("title");
var info = box.text(content);
$(this).stop().animate({
'backgroundPosition' : " -20px -60px "
},90);
info.show();
}).mouseleave(function() {
var box = $(this).parents(".workshop_img").find(".coverInfo");
var content = $(this).attr("title");
var info = box.text(content);
$(this).stop().animate({
'backgroundPosition' : "-40px 0"
},200);
info.stop().hide();
});
});
It has just been clean, but do not work fine.
What's the problem?
The new box shows immediately because it is not initially marked as hidden. .fadeIn() only fades in something that is initially not showing.
You can make it initially not visible like this:
$("<div class='coverInfo'></div>").text(content).hide().prependTo($(this).parent()).fadeIn("fast");
You also can get rid of the .each() iterator you're using. You don't need it. You can just use:
$(".coverImg").css(...).mouseover(...).mouseout(...);
You don't need the .each() at all.
I'd also suggest you use .hover(fn1, fn2) instead of .mouseover(fn1) and .mouseout(fn2).
And, it looks like you are creating a new object and inserting it on every mouseover event such that multiple such objects will pile up in the page. You should either .remove() the object in the mouseout function or you should reuse a previously existing element if it exists in the element rather than creating more and more of them.
Sometimes when you are using the events for mouse hovering and you are also changing the page, the change to the page can cause the element to lose the mouse hover which then hides the change to the page and then it all starts over again. I can't tell for sure if that is happening in your case (I'd need a working example to play with to see), but it seems possible.

Categories

Resources