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.
Related
I wanted to have one element highlight either when it gets hovered, or some other element is also hovered. Yet the code i've written to achieve this seems to override the hover pseudo-class whenever it gets run. I can't seem to see why -- minimal example in this fiddle: https://jsfiddle.net/mLynfz3x/
As soon as the second element gets hovered, the hover pseudo class for the first one is removed, and I'm not sure why. Is it intended that the jQuery .css() function override pseudo-classes? Or is the issue something else that I've missed entirely
Thank you!
The set Color for the Element Testlink doesnt disable the hover-pseudo class, the fixed color for that element is just, lets say "higher priority". So all you gotta do to fix it is add:
#testLink:hover {
color: olive !important;
}
and it should work with your existing JQuery.
This is what I did
$("#aTestItem").hover(() => {
$("#testLink").css("color", "olive");
}, () => {
$("#testLink").css("color", "black");
});
$("#testLink").hover(() => {
$("#testLink").css("color", "olive");
}, () => {
$("#testLink").css("color", "black");
});
At the top of a page I'm building,
There will be some navigation buttons for navigating internally within the page.
When hovered over, a button will change its appearance (for instance, the font color and the color of the button itself will switch places).
How do I make the buttons change their appearance in the same way once the user scrolls down to the part of the page the button links to? For instance, there is a button labeled "Contact" that links links to the "Contact" section of the page. How should one change the button's font color from (for instance) white to blue and the background color from blue to white when the user scrolls down to that part of the page?
This was already discussed elsewhere, but I'm not yet familiar with headroom.js.
We could use a simple onclick color change behavior with jquery since everything else is already working fine. So what you could do is just change the color on click and remove it from the previous one simontaneously.
For the following HTML:
<nav>
<li id="1"><a href="#" >One</a></li>
<li id="2"><a href="#" >Two</a></li>
<li id="3"><a href="#" >Three</a></li>
<li id="4"><a href="#" >Four</a></li>
</nav>
If onclick color remains same for every nav item then
jQuery(document).ready(function($) {
$("nav li").each(function() {
$(this).click(function() {
$(this).prev().find("a").css("color", "red");
$(this).next().find("a").css("color", "red");
$(this).find("a").css("color", "blue");
});
});
});
If each color is differrent.
jQuery(document).ready(function($) {
$("#1").click(function() {
$(this).prev().find("a").css("color", "red");
$(this).next().find("a").css("color", "red");
$(this).find("a").css("color", "blue");
});
})
Instead of .css you can also add a class like
jQuery(document).ready(function($) {
$("nav li").each(function() {
$(this).click(function() {
$(this).prev().find("a").removeClass("active");
$(this).next().find("a").removeClass("active");
$(this).find("a").addClass( "active" );
});
});
});
https://jsfiddle.net/yuhazt0p/1/
I'm using <a href> element along with :target css selector to show a <div> which by default is set to display:none. Problem is, that when I click on the link to show that <div>, it is automatically scrolling down my site towards that <div>.
Is there a way to stop the screen movement?
Unfortunately I am not yet proficient in anything besides CSS and HTML.
You can use event.preventDefault() to avoid this. Something like this:
$('a.yourclass').click(function(e)
{
//your code
e.preventDefault();
});
OR:
link
in the link enter:
Link here
You'll need JS anyway:
// (in jQuery)
$el.on('click', function(e) {
// find current scroll position
var pos = document.body.scrollTop || document.documentElement.scrollTop;
// let normal action propagate etc
// in the next available frame (async, hence setTimeout), reset scroll posiion
setTimeout(function() {
window.scrollTo(0, pos);
}, 1);
})
I don't know if this will flicker the screen. It might. It's a horrible hack either way.
In my Chrome, there's no flicker: http://jsfiddle.net/rudiedirkx/LEwNd/1/show/
There are two ways to tell the browser we don't want it to act:
The main way is to use the event object. There's a method
event.preventDefault().
If the handler is assigned using on (not by
addEventListener), then we can just return false from it.
Example:
Click here
or
here
This is a bit of a hack but you could use a basic css work around:
CSS only Example
#div1 {
height: 0;
overflow:hidden;
}
#div1:target {
height: auto;
margin-top: -110px;
padding-top: 110px;
}
#div2 {
background:red;
}
Click to show
<div id="div1">
<div id="div2">Content</div>
</div>
If you need it to be a little more flexible you can add some js...
More Flexible Example with JS
$('a').click(function () {
$('#div1').css({
'margin-top': 0 - $('#div1').position().top + $(window).scrollTop(),
'padding-top': $('#div1').position().top - $(window).scrollTop()
});
});
Basically you're pulling the top of div1 up with the negative margin and then pushing div2 back down with the padding, so that the top of div1 rests at the top of the window... Like I said its a hack but it does the trick.
Those links are anchor-links and by default made for those jumps :) You could use JS to prevent the default behaviour in some way. For example using jQuery:
$('a').click(function(e){e.preventDefault();});
or by default add return false; to the links
Avoid using :target all together and just use onclick event.
function myFunction()
{
document.getElementById('hiddenDiv').style.display = 'block';
return false;
}
I've got three elements with IDs "albums", "about", and "contact", and three links to show/hide them via the toggle() function, with IDs "togglealbums", "toggleabout", and "togglecontact". I only want one of these elements to be able to be seen at any time, so I wrote the following functions:
$('#togglealbums').click(function() {
if( $('#about').is(':visible') ) {
$('#about').toggle(function() {
$('#albums').toggle();
});
} else if( $('#contact').is(':visible') ) {
$('#contact').toggle(function() {
$('#albums').toggle();
});
} else {
$('#albums').toggle();
}
});
$('#toggleabout').click(function() {
if( $('#albums').is(':visible') ) {
$('#albums').toggle(function() {
$('#about').toggle();
});
} else if( $('#contact').is(':visible') ) {
$('#contact').toggle(function() {
$('#about').toggle();
});
} else {
$('#about').toggle();
}
});
$('#togglecontact').click(function() {
if( $('#albums').is(':visible') ) {
$('#albums').toggle(function() {
$('#contact').toggle();
});
} else if( $('#about').is(':visible') ) {
$('#about').toggle(function() {
$('#contact').toggle();
});
} else {
$('#contact').toggle();
}
});
First of all, if these are wildly inefficient or there is an easier way to do this, please let me know.
What I've found is that if none of the three DIVs is visible, clicking one of the toggle links will show/hide the respective div with no animation. However, if one of the DIVs is visible, clicking another toggle link will cause the div to shrink and fade and the new one expands and fades in, which I don't want (at least for now). This can be seen here: http://new.e17.paca.arvixe.com.
Can anyone tell me why this is happening?
Thanks!
EDIT:
Markup is here:
<body>
<div id="main">
<div id="nav">
<div id="menu">
<ul>
<li>Albums</li>
<li>About Me</li>
<li>Contact</li>
</ul>
</div>
</div>
<div id="albums">
Albums go here
</div>
<div id="about">
About info goes here
</div>
<div id="contact">
Contact info goes here
</div>
</div>
</body>
Your shrink-and-fade was happening because, if .toggle() is given a callback function, it assumes you want to animate the toggle instead of just switching it on/off. (According to the docs, as of this writing, that's only supposed to happen when you provide a duration. I've submitted a bug report about this.)
See http://jsfiddle.net/mblase75/byKeP/1/ for a reduced example of this. To solve it, just remove the callbacks and put the same code in the next line of your function.
As for streamlining your code, classes are your friends. HTML:
toggle albums
toggle about
toggle contact
<div class="toggleblock" id="albums">ALBUMS</div>
<div class="toggleblock" id="about">ABOUT</div>
<div class="toggleblock" id="contact">CONTACT</div>
Note the data- attributes, which jQuery will parse and make accessible through the .data() method. This makes it easy to store a unique div ID on the hyperlink itself, which in turn streamlines our JavaScript immensely. JS:
$('.togglelink').on('click',function(e) {
var id = $(this).data('block');
$('#'+id).toggle().siblings('.toggleblock').hide();
});
http://jsfiddle.net/mblase75/byKeP/
i have a ul-Element with 30 li-Elements, but only 20 Elements are shown, the overflow is hidden. Now i want to show the last 20 Elements. Is there a translateY(X)-Method for the ul-Element or something else?
Fesp
This solution is shooting into that big vague question. Hopefully it hits!
<ul id="ul_id" style="height:200px;overflow:hidden;">
<li></li>
...
</ul>
<script type="text/javascript">
document.getElementById('ul_id').scrollTop=150;
</script>
You can scroll a div with "scrollTop" in javascript. Just set the desired offset in pixels.
You'll need to scroll the element to reveal children that are "below the fold". Depending on if they're stacked on top of each other or in a row you'll need to use ul.scrollTop or ul.scrollLeft (from JavaScript).
Not a CSS HTML method I know. But the only way I can think to do what you ask (if I'm guessing correctly) is via jQuery, so.....
This will dynamically add an additional li with "See More..." when clicked, it will show more items on the list.
$(function() {
var hiddenStuff = $('.list li:gt(5)').hide();
if (hiddenStuff.size() > 0) {
var theCaption = '... See More';
$('.list').append(
$('<li class="more" id="toggler">' + theCaption + '</li>')
.toggle( function() {
hiddenStuff.slideDown(1000);
$(this).text('...See Less');
},
function() {
hiddenStuff.slideUp(1000);
$(this).text(theCaption);
})
);
}
});
jsFiddle Demo HERE
A CSS-only solution, which may work for you here: CSS-ONLY DEMO