Hover text change opacity on div jquery - javascript

I have a list panel with different words each word explains a div to the right.
I'm trying to change the opacity of the matching div to 1.0 when hovering the matching word.
<html> #HTML SETUP
<div class="Settings">
<ul>
<li class="SettingsSlideshow">
<h3>Slideshow</h3>
</li>
<li class="SlideshowImage">
<h4>Image</h4>
</li>
<li class="SlideshowTitle">
<h4>Title</h4>
</li>
</ul>
</div>
<div class="Slideshow">
</div>
</html>
<script type="text/javascript"> #JAVASCRIPT
$(function(){
$(".SettingsSlideshow").hover(function(){
$(".Slideshow").css({opacity: 1.0});
});
});
</script>
This is just one example of what I'm trying to achieve. This don't work, Do I need to define x:hover in css as well? I have defined a opacity: 0.7 on each div in css. Mabye I need to define it: (".Settings ul li SettingSlideshow")?
Thanks for help!

<script type="text/javascript">
$(function(){
$(".SettingsSlideshow").hover(function(){
$(".Slideshow").css({opacity: 1.0});
},function(){
$(".Slideshow").css({opacity: 0.0});// here set your rest opacity between 0 to 1
});
});
</script>
reference hover

Related

jQuery toggling display css attribute

I'm trying to get one of my links to dynamically show when an anchor href link gets clicked on.
The JavaScript code to show the cats link but it's not showing the cats link as follows:
$(".button").click(function() {
$("#cat").show();
});
ul > li > #cat {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li>Dogs</li>
<li><a id="cat" href="javascript:void(0);">Cats</a></li>
</ul>
<p>Cool cats are stored here</p>
Press here to enable Cats
you'll need display:hidden as default CSS for #cat, then $("#cat").show() will have effect. If you want the button to toggle the show state then use $("#cat").toggle()
You can hide the link on page load by default using $("#cat").hide();in the $(document).ready(function(){}
Check the snippet. Hope this helps
$(document).ready(function(){
$("#cat").hide();
$(".button").click(function() {
$("#cat").show();
});
});
<ul>
<li>Dogs</li>
<li><a id="cat" href="javascript:void(0);">Cats</a></li>
</ul>
<p>Cool cats are stored here</p>
Press here to enable Cats
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
Giving this a quick demo in codepen, the following code (and codepen example) would be how I'd set up what you're trying to do.
The HTML:
<ul>
<li>
<a href="dogs.html">
Dogs
</a>
</li>
<!-- you want to hide the LI, because if you hide the <a>, then
there will be an empty <li> that you'll need to deal with in
the layout of your page / screen reader users may end up
wondering why there is an empty list item
-->
<li id="cats_item" class="display-none">
<a href="cats.html">
Cats
</a>
</li>
</ul>
<p id="reveal_message">
Reveal a link to cats via the following button:
<!--
Use a button here since you're triggering an action
on the page, and not actually linking anywhere.
-->
<button type="button" id="reveal_button">
Reveal Cats
</button>
</p>
The CSS
/* this is all you need to hide any element */
.display-none {
display: none;
}
The jQuery
$('#reveal_button').on('click', function () {
// reveal the LI that contains the cats link
$('#cats_item').show();
/* hide the reveal message, since cats is shown,
this no longer has any usefulness (or change the message
and action to a 'toggle' to show/hide the cat link
*/
$('#reveal_message').hide();
/*
move keyboard focus to the cats link, otherwise
keyboard users would have to traverse back up the
DOM to get to the newly revealed link
*/
$('#cats_item a').focus();
});
Here is a codepen that reveals the cat link on button click:
http://codepen.io/scottohara/pen/VbYyGr
Try the following code
CSS
ul > li > #cat {
display: none;
}
JQUERY
$(".button").click(function() {
$("#cat").toggle();
});
Here is the working code: https://jsfiddle.net/u0ajrpw0/2/
Did you add the jQuery Cdn in the html file?
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
Or you can use this to hide as well.
$('#cat').hide();
Your code should work if isn't dynamically generated, if so you should use event delegation on() :
$("body").on("click", ".button", function() {
$("#cat").show();
});
NOTE: Make sure that your code is wrapped by ready function.
Hope this helps.
$(function(){
$(".button").click(function() {
$("#cat").show();
});
});
ul > li > #cat {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li>Dogs</li>
<li><a id="cat" href="javascript:void(0);">Cats</a></li>
</ul>
<p>Cool cats are stored here</p>
Press here to enable Cats

Selector :not in jQuery not working

I have a body with ID body and inside it a div with class nav-container. I want to remove certain classes when users click on the #body, but not .nav-container (it's an overlay type of menu).
Tried below code
HTML:
<body id="body">
<div class="nav-container">
X
<nav class="display">
<ul>
<li> One </li>
<li> Two </li>
<li> Three </li>
</ul>
</nav>
</div>
</body>
jQuery
$('#body :not(.nav-container)').click(function() {
$('.cover').removeClass('active-cover');
$('.nav-container').removeClass('active');
});
It does not seem to be working for me though.
It wont work as not exclude the selected elements which pass the earlier css-selector criteria since .nav-container is not part of list that is selected by #body (wont be list in this case as its ID), so you wont be able to exclude that.
So basically what you need is
$(document).on("click", "div:not('.nav-container')",function() {
$('.cover').removeClass('active-cover');
$('.nav-container').removeClass('active');
});
the first element shouldn't be the parent, like #body, rather the element. for example div:not('example') works for every div except example.
$("div:not(.nav-container)").click(function() {
alert("nav-container was not clicked");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body id="body">
<div class="nav-container">
X
<nav class="display">
<ul>
<li> One </li>
<li> Two </li>
<li> Three </li>
</ul>
</nav>
</div>
<br />
<div>click here </div>
</body>

On click highlight the tab and display the content

currently I'm trying like if I click on the tab it will change color instead of the standard color I set.How do I go about doing it because I'm using the toggle() function.
Currently I'm stuck at here.
$("li").click(function(){
$(this).css('background-color',"#6F0")
})
http://jsfiddle.net/eMLTB/112
Hide all .tabContent elements on page load and show them on click of li.
Write:
JS:
$("li").click(function () {
$($(this).find("a").attr("href")).toggle();
$(this).toggleClass("active");
});
CSS:
.active {
background-color:#6F0;
}
.tabContent {
display:none;
}
DEMO here.
Check the line of script where i placed a comment, I think after adding this solves your problem.
<div id="container">
<nav id="tabs">
<ul id ="ta" class="nav">
<li id="a">0</li>
<li id="b">5</li>
<li id="c">10</li>
</ul>
<div class="tabContent" id="tabs-1" >
<h2>Testing1</h2>
</div>
<div class="tabContent" id="tabs-2">
<h2>Testing2</h2>
</div>
<div class="tabContent" id="tabs-3">
<h2>Testing3</h2>
</div>
</nav>
And in your Script
var i;
$("li").each(
function(){
$(this).click(
function(){
i = $(this).find("a").attr("href");
$(i).toggle();
}
);
}
);
$("li").click(function(){
$('li').css('background-color',"#fff");//------>I set it #fff , you Can Put here your standard color code
$(this).css('background-color',"#6F0")
})
First, put the class attribute for <li> tag, example :
<ul id ="ta" class="nav">
<li id="a" class="navi">0</li>
<li id="b" class="navi">5</li>
<li id="c" class="navi">10</li>
</ul>
next, make your code like this :
$("li").click(function(){
$(".navi").css('background-color', "");
$(this).css('background-color', "#6F0");
});
I would suggest using the .toogleClass() method.
It is similar like .toogle() but better for CSS manipulation.
First create class in your CSS that will represent your color and then toogle that class.
http://api.jquery.com/toggleClass/

jQuery .show & .hide not working

I am currently working on building a small menu that will change divs based upon which one it clicked. So if one is clicked it will show the div associated with it and hide the others, ect. But I cannot get it to work, nor can I figure out why. Any help would be appreciated. Thanks.
Below is my code. I've clipped out the content as there was a lot of it.
<script type="text/javascript">
$('.mopHeader').click(function() {
$('#raid-progress-mop').show();
$('#raid-progress-cata').hide();
$('#raid-progress-wotlk').hide();
$('#raid-progress-tbc').hide();
$('#raid-progress-vanilla').hide();
});
$('.cataHeader').click(function() {
$('#raid-progress-mop').hide();
$('#raid-progress-cata').show();
$('#raid-progress-wotlk').hide();
$('#raid-progress-tbc').hide();
$('#raid-progress-vanilla').hide();
});
$('.wotlkHeader').click(function() {
$('#raid-progress-mop').hide();
$('#raid-progress-cata').hide();
$('#raid-progress-wotlk').show();
$('#raid-progress-tbc').hide();
$('#raid-progress-vanilla').hide();
});
$('.tbcHeader').click(function() {
$('#raid-progress-mop').hide();
$('#raid-progress-cata').hide();
$('#raid-progress-wotlk').hide();
$('#raid-progress-tbc').show();
$('#raid-progress-vanilla').hide();
});
$('.vanillaHeader').click(function() {
$('#raid-progress-mop').hide();
$('#raid-progress-cata').hide();
$('#raid-progress-wotlk').hide();
$('#raid-progress-tbc').hide();
$('#raid-progress-vanilla').show();
});
</script>
<span class="h4">Raid Progress <span class="mopHeader">MoP</span> <span class="cataHeader">Cata</span> <span class="wotlkHeader">WotLK</span> <span class="tbcHeader">TBC</span> <span class="vanillaHeader">WoW</span></span>
<div id="raid-progress-mop">
<ul id="raid-mop">
<li>Content A</li>
</ul>
</div>
<div id="raid-progress-cata">
<ul id="raid-cata">
<li>Content B</li>
</ul>
</div>
<div id="raid-progress-wotlk">
<ul id="raid-wotlk">
<li>Content C</li>
</ul>
</div>
<div id="raid-progress-tbc">
<ul id="raid-tbc">
<li>Content D</li>
</ul>
</div>
<div id="raid-progress-vanilla">
<ul id="raid-vanilla">
<li>Content E</li>
</ul>
</div>
Wrap your code in:
$(function(){ ... });
...which is the short form of:
$(document).ready(function(){ ... });
Cheers
You need to put the script underneath your markup. Either that, or put it inside document.ready callback:
$(document).ready(function() {
// code here
});
The problem is that when the script appears above the markup, it will execute before the HTML is loaded, and so the browser won't yet know about raid-progress-mop, etc.
How about doing that a little more dynamically inside a ready() function :
<script type="text/javascript">
$(function() {
$('[class$="Header"]').on('click', function() {
var myClass = $(this).attr('class').replace('Header', '');
$('[id^="raid-progress"]').hide();
$('#raid-progress-' + myClass).show();
});
});
</script>
jsBin demo
Wrap your code into a ready finction and this code I wrote is all you need:
$(function(){
$('span[class$="Header"]').click(function(){
var classNameSpecific = $(this).attr('class').split('Header')[0];
$('div[id^="raid-progress-"]').hide();
$('#raid-progress-'+classNameSpecific).show();
});
});
Explanation:
$('span[class$="Header"]') = target any span element which class ends with Header
Now just attach a click handler to all that spans.
Than, to hide all your div elements do:
$('div[id^="raid-progress-"]').hide(); = will hide any div which id starts with raid-progress-
and than you just need to target the div that contains the magic word:
$('#raid-progress-'+classNameSpecific).show();
$('.mopHeader') isn't defined yet. wrap your script with $(function(){...})

js 'toggle' or 'hover' function?

I don't want to use the toggle, what would I need to use to get the following nav structure to stay put when main link is hovered over?
Current js:
<script type="text/javascript">
$(document).ready(function(){
$(".downservices").hover(function(){
$(".servicesdropped").toggle("fast");
});
});
</script>
Sample page
(Notice that when the submenu pops up, I cannot click on the links, as the submenu fades away)
If you aren't fussed about animation, and you wish to use JQuery you can toggle the CSS visibility rule on the class.
$(document).ready(function()
// Make sure the item is hidden initially, best to do
// this in CSS.
$(".servicesdropped").css("visibility", "hidden");
{
$(".downservices").hover(function()
{
$(".servicesdropped").css("visibility", "display");
},
function()
{
$(".servicesdropped").css("visibility", "hidden");
});
});
Using visiblity means the element will still consume the space it does in the DOM but does not display making sure the structure and positioning of other elements surrounding it are left in tact. The downside is that animations such as fadeIn() and fadeOut() will not work.
Your html markup architecture of menu should like this:
<ul>
<li class="downservices">GUYS
<div class="servicesdropped" style="display: none;">
<ul class="middle">
<h3>Shirts & Tanks:</h3>
<li>MuSkull</li>
<li>Bamboo Athletic Tank</li>
<li>Thin Strap Tank</li>
</ul>
<ul class="right">
<h3>Other Stuff:</h3>
<li>Shorties</li>
<li>Hoodies</li>
<li>Socks</li>
<li>Hats</li>
</ul>
</div>
</li>
<li>products</li>
<li>portfolio</li>
<li>contact</li>
</ul>
And in the script use this:
$(document).ready(function(){
$("li.downservices").hover(function()
{
$(this).find(".servicesdropped").slideDown("fast");
},
function()
{
$(this).find(".servicesdropped").slideUp("fast");
});
});
use like this
<script type="text/javascript">
$(document).ready(function(){
$(".downservices").hover(function(){
$(".servicesdropped").slideDown();
});
});
</script>
for hover out the menu disappear use this
<script type="text/javascript">
$(document).ready(function(){
$(".downservices").hover(
function(){
$(".servicesdropped").slideDown();
},
function(){
$(".servicesdropped").slideUp();
}
);
});
</script>

Categories