Rotate image when hover on other div - javascript

I'm trying to create something real simple. But I don't know why it isn't working?
This is what I'm trying: http://www.yannickluijten.be/test
when I hover the green, blue, gray or yellow div the image in the middle has to rotate.
Here is the code:
$(document).ready(function() {
$('#green').hover(function(){
$('#rotator').addClass('rotate_green');
},
function(){
$('#rotator').removeClass('rotate_green');
});
$('#blue').hover(function(){
$('#rotator').addClass('rotate_blue');
},
function(){
$('#rotator').removeClass('rotate_blue');
});
$('#gray').hover(function(){
$('#rotator').addClass('rotate_gray');
},
function(){
$('#rotator').removeClass('rotate_gray');
});
$('#yellow').hover(function(){
$('#rotator').addClass('rotate_yellow');
},
function(){
$('#rotator').removeClass('rotate_yellow');
});
});

You forgot to include jQuery.js on your code.
Add this line in HEAD section
http://code.jquery.com/jquery-1.9.1.js

Related

Jquery animate created element

I made div, if i click on it, jquery makes bullet and that element is animated. This is code:
$('.square').click(function() {
$('<div class="bullet"></div>').appendTo($('body')).animate({
'margin-top': 554
}, 2000, function() {
$(this).remove();
});
});
It works properly when I'm not clicking second time on div before animation is done. If i do this, my second "bullet" starts animation from position of first.
How to fix that? Thank's for help :)
UPDATE##
Here's the jsfiddle with problem:
https://jsfiddle.net/2ghj1x45/
it's because the elements all have a size because they aren't positioned absolutely so each bullet div you add has display block, so will get it's own line where it's height is bullet size + margin top , which increases as it's animated. try instead using position absolute so the bullet div doesn't affect the layout of any other div
like so
$(bullet).animate({ top: value });
Why not timeout the click function with a variable:
var animating = false;
$('.square').click(function() {
if(!animating) {
animating = true;
setTimeout(function() {
animating = false;
}, 2000);
$('<div class="bullet"></div>').appendTo($('body')).animate({
'margin-top': 554
}, 2000, function() {
$(this).remove();
});
}
});
EDIT:
Updated JSfiddle

Swap and save background color with jquery

I have a menu where on every hover of its links the background color of my page changes.
Now, let's say that my default index.html color is blue, and when I hover on about and contact I have yellow and red.
If I click on the about page its background color will be yellow, and if I hover on home it will go back blue.
What happens is when I mouseOut back to the page, the background changed to blue again, same if I'd hovered on contact.
Here there's my code in JS
$(document).ready(function() {
$('#about').hover(function() {
$('body').css('background-color', 'yellow');
}, function() {
$('body').css('background', '');
});
$('#contact').hover(function() {
$('body').css('background-color', 'red');
}, function() {
$('body').css('background', '');
});
And here there is the menu
<a id="about" href="about.html">about</a>
<a id="contact" href="contact.html">contact</a>
I assume I should do something like check if the current background color matches the hovered one, on mouseOut keep that color, otherwise set it to the initial one.
I'm not an expert of JS, especially when it comes to conditions.
Anybody could help me out?
Thanks!
If you want the color to stay the same after hovering, then you should remove the second parameter of hover():
$(document).ready(function() {
$('#about').hover(function() {
$('body').css('background-color', 'yellow');
});
$('#contact').hover(function() {
$('body').css('background-color', 'red');
});
});
https://jsfiddle.net/as9jpopd/
** EDIT **
If you want the color only to change when the user moves the mouse over the link and then back, then your code should work, only that closing brackets are missing:
$(document).ready(function() {
$('#about').hover(function() {
$('body').css('background-color', 'yellow');
}, function() {
$('body').css('background', '');
});
$('#contact').hover(function() {
$('body').css('background-color', 'red');
}, function() {
$('body').css('background', '');
});
});
Thanks to Joachim, but unfortunately that did not work. I uploaded an early stage of the website so you can take a look at the problem and the behaviour of the bg. http://mircofragomena.com/
As you see on the front page the bg is white, but when you hover changes. If you click on pickabrew, you will see the page in yellow as when hovered, but if you go again on the menu hover/out, the page goes back in white as the front page. It should stay yellow though, as it should happen with every other color/page.
I'm now using this other code
$(function() { // Shorthand for $(document).ready(function() {
var body = $('body');
var bg = body.css('background-color');
$('ul.nav a').hover(function() {
body.css('background-color', $(this).data('bgColor'));
}, function() {
body.css('background-color', bg);
});
});
related with this one on the links
<li>Posters</li>
though it does not work.
Is this a good starting code?

Hover out from two divs simultaneously

Have a look at the following JQuery code:
function my_hover()
{
$( "#up,#down" ).hover(
function() {
$("#up").animate({
"background-color": "#020306"
});
$("#down").animate({
"background-color": "#171716"
});
},
function() {
$("#up").css("background-color","#C8CACF" );
$("#down").css("background-color","#FAFAF8" );
}
);
}
There are two divs: #up,#down which I cannot wrap into a parent div(due to design restrictions). What I want to do is to animate the change in background color whenever #up or #down are being hovered. However, in case the mouse leaves one div by going directly to the other(the two divs are stuck together vertically) I do not want the last two lines of the above code being applied. I want them to be applied only when mouse hovers out from both divs. How may I achieve that? At users.sch.gr/ellhn you may see what is happening with the above code in the first left rectangle with the target photo (transition between up and down provokes change of color and that's not desirable).
Thank you
Try this.
HTML
<div id="up">Up</div>
<div id="down">down</div>
CSS
div{ transition:all 0.25s ease-in-out;}
#up{background:#C8CACF;}
#down{background:#FAFAF8;}
#up.up-hover{background-color:green;}
#down.down-hover{background-color:yellow;}
Script
$('#up, #down').mouseenter(function(){
//alert('hi')
$("#up").addClass('up-hover');
$("#down").addClass('down-hover');
})
.mouseleave(function(){
//alert('hi')
$("#up").removeClass('up-hover');
$("#down").removeClass('down-hover');
});
Fiddle Demo
Using the technique #nnnnn alluded to, e.g., using a timeout:
(function init() {
var $up = $('#up'),
$down = $('#down'),
hovered = false;
$up.hover(over, out);
$down.hover(over, out);
function over() {
hovered = true;
$up.css({
"background-color": "#020306"
});
$down.css({
"background-color": "#171716"
});
}
function out() {
setTimeout(function to() {
if (!hovered) {
$up.css("background-color", "#C8CACF");
$down.css("background-color", "#FAFAF8");
}
}, 1000);
hovered = false;
}
})();
http://jsfiddle.net/TudqT/3/
And with the elements inline next to each other, instead of vertically aligned:
http://jsfiddle.net/TudqT/5/

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/

Jquery Animated Tab - revert on mouseout

I'm trying to adapt Gaya Design's Animated Tabbed Content http://www.gayadesign.com/diy/animated-tabbed-content-with-jquery/ for a navigation menu. I want to have the background verting back to the first tab on mouseout but can't seem to get that working. Below is the Javascript for moving background function:
var TabbedContent = {
init: function() {
$(".tab_item").mouseover(function() {
var background = $(this).parent().find(".moving_bg");
$(background).stop().animate({
left: $(this).position()['left']
}, {
duration: 300
});
TabbedContent.slideContent($(this));
});
},
}
$(document).ready(function() {
TabbedContent.init();
});
And here is a link to what I'm trying to achieve. Link
Any help on how to get the background to revert to tab one on mouseout will be very much appreciated!
Thanks
What about triggeting the mouseover event of the first item:
$('.tabs').mouseout(function(){
$(this).find('.tab_item:first').mouseover();
});

Categories

Resources