I'm trying to create an effect where when you hover over a list item it changes the opacity of an image (in a completely different div) from 0 to 1. I have no trouble doing it in CSS when hovering over the img itself or its parent elements. But this is stumping me. Here's what I have (I'm really new to jquery, so it may be all wrong).
<style>
#img-nav img {opacity:0.0;}
#img-nav:hover img {opacity:1.0;}
</style>
<div id=header>
<ul id="nav">
<li id="one">item1</li>
<li id="two">item2</li>
<li id="three">item3</li>
</ul></div>
<ul id="img-nav">
<li><img src="one.jpg" id="img-one"/></li>
<li><img src="two.jpg" id="img-two"/></li>
<li><img src="three.jpg" id="img-three"/></li>
</ul>
And my questionable jquery:
$("#one, #two, #three").hover(function(){
$("#img-one, #img-two, #img-three").css({ opacity:1.0 });
});
I guess one thing that's wrong is that I need three different hover declarations for each of the three li/img combinations. Like I said, I'm very new to jquery, so sorry if the answer is simple. I did search the boards and couldn't find a solution. Of course, I'd rather find a css solution but I don't think there is one.
Update/Solution:
#Jason. Here's your jquery changed a little to do exactly what I wanted. I got rid of the first declaration since I already had opacity set to 0 in the CSS and didn't need jquery to do it. Then hovering over the li changes the images opacity with .css. The issue was using .css to change the opacity back to 0. It was keeping the inline style declaration, which was screwing with the rules in my stylesheet. So now when hover ends, I just remove the inline style attribute altogether with .removeAttr ('style').
Thanks for the help!
$("#one").hover(function () {
$('#img-one').css({opacity : 1.0});
},
function () {
$('#img-one').removeAttr("style");
}
);
$("#two").hover(function () {
$('#img-two').css({opacity : 1.0});
},
function () {
$('#img-two').removeAttr("style");
}
);
$("#three").hover(function () {
$('#img-three').css({opacity : 1.0});
},
function () {
$('#img-three').removeAttr("style");
}
);
There is probably a more elegant way to do this... but the quick and dirty:
$("#img-one, #img-two, #img-three").css('opacity','0');
$("#one").hover(function () {
$('#img-one').css({opacity : 1.0});
},
function () {
$('#img-one').css({opacity : 0.0});
}
);
$("#two").hover(function () {
$('#img-two').css({opacity : 1.0});
},
function () {
$('#img-two').css({opacity : 0.0});
}
);
$("#three").hover(function () {
$('#img-three').css({opacity : 1.0});
},
function () {
$('#img-three').css({opacity : 0.0});
}
);
http://jsfiddle.net/jasongennaro/KdhPG/
Your code looks like :
$(".one, .two, .three").hover(function(){
$(".img-one, .img-two, .img-three").css({ opacity:1.0 });
});
it should be:
$("div#header li").hover(function() {
$("#img-one, #img-two, #img-three").css({ opacity:1.0 });
},function () {
$("#img-one, #img-two, #img-three").css({ opacity:0.0 });
});
In Jquery you can call item by its id by denoting with #sign and class with .(dot) sign
NEW UPDATE:
See the final demo as you want: http://jsfiddle.net/rathoreahsan/7NCQu/20/
Related
I have a little problem with this: http://jsfiddle.net/eyy0c4uf/
I would like to do the following.
image-logo will be hidden untill user hover over the main-image. I also would like to have another hover effect for image-logowhile the main-image hover effect is keept.
CSS/HTML/JS solutions only.
Thanks
This answer is based on the inferred intent of the author after looking at their attached code.
You cannot hover two siblings at the same time, but you can cause a delayed snapback from a hover.
.logo-image {
opacity: 0;
transition: opacity 10000s, box-shadow 2s;
}
.main-image:hover + .logo-image {
opacity: 1;
transition: opacity 1s;
}
.logo-image:hover {
box-shadow: 0 1em 2em rgba(0,0,0,.5);
}
The transition delay on .logo-image will cause it to retain its second state for 10000 seconds. This number is arbitrarily chosen - we just need it to last a while.
So you only need to hover the .main-image element, which then reveals its sibling. The sibling has its own :hover state that can then be interacted with.
Fiddle: http://jsfiddle.net/jonathansampson/eyy0c4uf/3/
Fair warning, this is a hack. I don't suggest you rely on an approach like this. Instead, identify the effect you're looking to achieve, and make the appropriate markup/scripting changes needed to appropriately satisfy that need.
This answer is merely a demonstration that this is, in some way, possible with CSS alone.
This is the jQuery function that you need. Don't forget to declare the jQuery in your header
http://jsfiddle.net/eyy0c4uf/4/
jQuery( document ).ready(function ( $ ) {
$( ".main-image" ).hover(
function () {
$( ".logo-image" ).css( 'display', 'block' );
},
function () {
$( ".logo-image" ).css( 'display', 'none' );
}
);
$( ".logo-image" ).hover(
function () {
$( ".logo-image" ).css( 'display', 'block' );
},
function () {
$( ".logo-image" ).css( 'display', 'none' );
}
);
});
This is an example of jQuery hover effect. Let me know if you want something more.
EDIT: I fixed a little bug that happens when you hover .logo-image
I have a cart on my website, I have maked a kind of graphic as image, that appear as bacground behind the cart when :hover.
But I was wondering how I can make this object fade/appear slowly on :hover, instead of instant as it is now.
I tried to copy the html and css from my website, but it looks bad in jsfiddle.
http://oliver.kaspertoxvig.dk/
I am not sure if this is the preferred way of doing it, but if you are using the jQuery library, this should do the trick:
$(function () {
$(".cart-css-class").hover(
function () {
// Fade in on hover
$(this).stop().animate({ "opacity": "1" }, "slow");
},
function () {
// Fade out when no longer hover
$(this).stop().animate({ "opacity": "0" }, "slow");
});
});
This assumes your element has the css class cart-css-class. To hide it on load, you should apply this CSS:
.cart-css-class {
opacity: 0;
}
I am making an ajax call to get more results and get them appended to the existing ones in the same page. While the contents are fetched, i need to show a scrolling spinner every time requests are send. In order to achieve this i go on adding a div at bottom of results fetched like this:
<div class="loader" style="display:block"></div>
And i am trying to show the spinner using the below code:
beforeSend: function()
{
$('#loader:last-child').html('<img src="/graphics/loading.gif" alt="Wait" />');
}
complete: function()
{
$('#loader:last-child').html('');
}
But i am not able to get the spinner effect, it is not at all shown on the page(results are though fetched).
Please help me in getting it corrected. Any other thoughts to have the spinner effect are also appreciated.
EDIT: I have removed quotes as they wre in my perl code. sorry for that.
Put a space in <imgsrc so that its <img src
Your selector should be $('.loader:last-child') since loader is a class not an id. (Use . instead of #)
Instead of adding any new elements or images, you could just add a class to the container of your results while it's loading, and use the class to add padding and the spinner graphic as the background image of the container. When the loading completes, remove the class:
beforeSend: function(){
$('#resultsContainer').addClass('loading');
}
complete: function(){
$('#resultsContainer').removeClass('loading');
}
Then your CSS could look like:
#resultsContainer.loading {
background: url(graphics/loading.gif) no-repeat center bottom;
padding-bottom: 2em;
}
For extra style, add transitions to padding-bottom on resultContainer, so that the loading graphic slides smoothly in and out.
Try this
beforeSend: function()
{
$("#loader").last().html('<img src="/graphics/loading.gif" alt="Wait" />');
}
complete: function()
{
$("#loader").last().html('');
}
why are you using \ slashes
try this
beforeSend: function()
{
$('.loader:last-child').html('<img src="/graphics/loading.gif" alt="Wait" />');
}
complete: function()
{
$('.loader:last-child').html('');
}
you have to use . for selecting class not # its for id and also , is missing between beforeComplete and complete.
You do not need to escape the quotes, also put a space between imgsrc like img src
As well mentioned by rob in his answer that
Your selector should be $('.loader:last-child') since loader is a class not an id. Use pereiod (.) instead of (#)
Also add a comma , after beforeSend: function(){}, and try;
Use the following code:
beforeSend: function() {
$('.loader:last-child').html('<img src="/graphics/loading.gif" alt="Wait" />');
},
complete: function() {
$('.loader:last-child').html('');
}
Try appending the loader image to the HTML of e #loader and then remove it.
(note that I added an id to the image)
beforeSend: function()
{
$('.loader').append('<img src="/graphics/loading.gif" id="waitSpinner" alt="Wait" />');
}
complete: function()
{
$('#waitSpinner').remove();
}
Edit: changed #loader to .loader, as that was the class.
Im trying to setup so that when you hover over class .object1 -> in turn should reveal .obj_1 when you are not hovered on it, it should hide .obj_1. I may be a little off in my code, thanks for the help!.
$(document).ready(function() {
$(".obj_1 , .obj_2").hide();
});
$(".object1").hover(
function() { $(".obj_1").show(); },
function() { $(".obj_2").hide(); }
);
$(".object2").hover(
function() { $(".obj_2").show(); },
function() { $(".obj_1").hide(); }
);
Very simple it should be
$(document).ready(function() {
$(".obj_1 , .obj_2").hide();
});
$(".object1").hover(
function() { $(".obj_1").show(); },
function() { $(".obj_1").hide(); }
);
$(".object2").hover(
function() { $(".obj_2").show(); },
function() { $(".obj_2").hide(); }
);
The "hover" handler function signature is ( mouseInHandler, mouseOutHandler).
For object1 you want to show obj_1 on mouseIn, and hide it on mouseOut.
You don't need to reference obj_2 on object1 hover handlers.
Check out the fiddle I made here
FYI - the hover events act weird when you have complex inner content. ( for example, div within another div and so on ). I advise you to use "mouseenter" and "mouseleave"
UPDATING ANSWER AFTER REALIZING THIS IS A DROP DOWN MENU QUESTION
The drop down menu in CSS is a great example where "hover" won't suffice --> because the submenu disappears once you're not on the link anymore.. and that's not what we want.
It is important to note 3 things about drop down menus :
They can (?should?) be achieved purely with CSS
The HTML structure is important.
For example, consider the following structure instead :
<ul class="menu">
<li>
</li>
<li>
<ul class="menu">
<li>
</li>
</ul>
</li>
</ul>
This structure is recursive - you can have infinite levels of submenus - and the mouseenter/mouseleave on the "li" will hold since the submenu is part of the "li" item.
To see this in action have a look in my fiddle
Please also note that I removed the first "hide" from the onload code, and replaced it with css "display:none" - which resolves flickering on page load ( flickering means - first the submenu shows, and once the page loads, we hide it. )
A css solution would include a selector with "hover" on it ( yes, hover.. )
You can find plenty of blog posts about it while searching in google.
Here is the first one I found.
Refer to this fiddle Please.
What I am trying to do is on hovering on a-tag inside #menu ul li, the background-color color of #header-bottom must also changed as of similar to background-color of the a-tag.
How can I achieve this?
UPDATE
Done this with CSS only yippie! :) Here is the fiddle
Here is a jQuery method (sorry too much effort to make this javascript only for me). It could be translated though.
http://jsfiddle.net/PCbVs/9/
$('.menu').hover(
function() {
var color = $(this).css('borderLeftColor');
console.log(color);
$('#header-bottom').css('backgroundColor', color);
}, function() {
});
Or with jQuery UI animated style transitions.
http://jsfiddle.net/PCbVs/10/
$('.menu').hover(
function() {
var color = $(this).css('borderLeftColor');
console.log(color);
$('#header-bottom').stop().animate({ backgroundColor: color }, 500);
}, function() {
});