I am trying to introduce new class for specific div element using nth-child() pseudo-class but it is not working!
Am I doing something wrong?
Note that menu, container, newClass are already defined in CSS section and I'm not showing them here.
Here is what I am trying:
$(function() {
$("button").click(function {
$("div:nth-child(2)").toggleClass("newClass");
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>Toggle CSS</button>
<div class="container">
<div class="menu">
<lu>
<li>option 1</li>
<li>option2</li>
<li>option 3</li>
</lu>
</div>
</div>
There are lots of mistakes in your code. Which div are you want to add a class newClass? I think your code should be like this
$(function() {
$("button").on('click', function() {
$(".menu").toggleClass("newClass");
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>Toggle CSS</button>
<div class="container">
<div class="menu">
<ul>
<li>option 1</li>
<li>option2</li>
<li>option 3</li>
</ul>
</div>
</div>
My mistake
I should not use
<script>
$("div:nth-child(2)").toggle Class("newClass");
</script>
this to introduce new class for div element, with the class menu , as it is not a second child of div . What given below is though working fine for me:
Correction
<script>
$("div.menu").toggleClass("newClass");
</script>
That is: to specify div element(whose class is going to be toggled) by tagging its class menu , to call jQuery for div with the class menu .
Also by mistake parentheses () were missing.
Related
I need assistance with changing css of div on hover event. My code:
<div class = "container-main">
<div class = "container-inner">
<ul class = "list">
<li>Option 1</li>
<li>Option 2</li>
</ul>
</div>
</div>
<div class = "container1">
<p>Container text</p>
</div>
<div class = "container2">
<p>Container text</p>
</div>
I need to be able to change classes container1 and container2 when hovering on ul or li in the main container. I only know how to change it with sibling containers but that's not what I need.
.container-main:hover + .container1 {background:red;}
Would appreciate any help in achieving this. Thanks.
Unfortunately, based on your markup heirarchy, what you're looking for isn't possible with just CSS. You can do this pretty simply with javascript though. Here's an example that will add a background-color to .container1 when you hover .container-main ul:
const list = document.querySelector('.container-main ul')
const container1 = document.querySelector('.container1')
list.addEventListener('mouseenter', () => container1.classList.add('red'))
list.addEventListener('mouseleave', () => container1.classList.remove('red'))
.red {
background-color: red;
}
<div class = "container-main">
<div class = "container-inner">
<ul class = "list">
<li>Option 1</li>
<li>Option 2</li>
</ul>
</div>
</div>
<div class = "container1">
<p>Container text</p>
</div>
<div class = "container2">
<p>Container text</p>
</div>
is this what you want?
jQuery solution:
$( ".container-main li" ).hover(
function() {
//do something when hovering
}, function() {
//do something after hovering
$(".container1").css("background", "red");
}
);
Thanks for your answers everyone. I was just unsure whether it was possible or not, I'll try to rearrange my div heirarchy. I know it can be done with jQuery but I have a task where I only need to use CSS without any programming languages.
Once again, thanks a lot. I got a better idea of what needs to be changed.
I don't think css alone can do this.
Do you have access to jquery?
https://jsfiddle.net/ifinto/o2gxgz9r/
$(document).ready(function() {
$(".container-main li").each(function(index) {
$(this).hover(
function() {
div = ".container" + (index + 1);
console.log("in_" + div);
$(div).css("background-color", "red")
},
function() {
div = ".container" + (index + 1);
console.log("out_" + div);
$(div).css("background-color", "white");
});
})
I have created a polymer element which contains slots. Each slot is intended to be replaced/filled with a HTML element with children and not just plain text. How can I style elements inside the slot and not only the element at top level of the slot? Or maybe, is it wrong to use elements with children for slots?
In example:
Template file:
<dom-module id="my-element">
<template>
<style>
.class-1 {
/* Styles here works! */
}
.class-1 ::slotted(ul) {
/* Styles here works! */
}
.class-1 ::slotted(ul) a {
/* Styles here doesn't work */
}
</style>
<div class="class-1">
<slot name="s1"></slot>
</div>
</template>
<script>
class MyElement extends Polymer.Element {
static get is() { return 'my-element'; }
}
window.customElements.define(MyElement.is, MyElement);
</script>
</dom-module>
HTML file:
<my-element>
<ul slot="s1">
<li>Link 1</li>
<li>Link 2</li>
</ul>
</my-element>
Thanks for your help!
Slotted does not allow sub selectors. Which means that
.class-1 ::slotted(ul) a
simply can not work because the 'a' is a subselection.
try this, but it seems taking effect inside all instance of my-element
<my-element><style>li a {color:#333}</style>
<ul slot="s1">
<li>Link 1</li>
<li>Link 2</li>
</ul>
</my-element>
I want to give hover effect to specific div, but that div is not a child of that div, which I hover
I have code, so, you can understand
$(".pannel-icon").click(function(){
$(".opening-menu").toggleClass('show');
});
body{overflow:hidden}
a{cursor:pointer}
.opening-menu{float:right;position:absolute;right:-100px; top: 50px;transition:0.2s all linear}
.main-menu{float:right}
.show{right:0;transition:0.2s all linear}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<div class="main-menu">
<span>coins:$100</span>
<a class="pannel-icon">
<span class="pannel-menu-hover">Menu</span>
</a>
</div>
<div class="opening-menu">
<ul>
<li>menu1</li>
<li>menu1</li>
<li>menu1</li>
<li>menu1</li>
</ul>
</div>
Inside this code, I want to hover .opening-menu when I hover .pannel-icon.
is it your answer?
$(".pannel-icon, .opening-menu").hover(function(){
$(".opening-menu").toggleClass('show');
});
body{overflow:hidden}
a{cursor:pointer}
.opening-menu{float:right;position:absolute;right:-100px; top: 10px;transition:0.2s all linear}
.main-menu{float:right}
.show{right:0;transition:0.2s all linear}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<div class="main-menu">
<span>coins:$100</span>
<a class="pannel-icon">
<span class="pannel-menu-hover">Menu</span>
</a>
</div>
<div class="opening-menu">
<ul>
<li>menu1</li>
<li>menu1</li>
<li>menu1</li>
<li>menu1</li>
</ul>
</div>
I think I see what you're saying, check this Fiddle: https://jsfiddle.net/nhnb9a5g/1/. If you hover over Menu, the menu appears but then disappears as soon as you try and click an item?
If so, check this revised fiddle: https://jsfiddle.net/zm93hL3e/3/
Add the following jQuery:
$(".pannel-icon, .opening-menu").hover(function(){
$(".opening-menu").toggleClass('show');
});
You could use a jquery hover function or selector to achieve this. But I prefer to place the opening-menu within the link. That way it's always hovered when you are hovering an item in the menu.
If I've understand correctly your question, do you want to change CSS properties in the class called opening-menu, right?
So, you can add in your CSS:
.opening-menu:hover{float:right;position:absolute;right:-100px; top: 50px;transition:0.2s all linear}
The complete CSS code will be:
body{overflow:hidden}
a{cursor:pointer}
.opening-menu:hover{float:right;position:absolute;right:-100px; top: 50px;transition:0.2s all linear}
.main-menu{float:right}
.show:hover{right:0;transition:0.2s all linear}
Alternatively, you can use a javascript function like this:
$(".pannel-icon").hover(hoverDiv);
function hoverDiv(){
$(".opening-menu").css({
'float':'right';
'position':'absolute';
'right':'-100px';
'top': '50px';
'transition':'0.2s'})
}
BACKGROUND
I would like to be able to hover over a link and a set of text fade out on this action
HTML
<nav class="PageNav">
<ul>
<li id="HomeLink">Home</li>
<li id="OverviewLink">Overview</li>
<li id="ServicesLink">
Mega Services
<ul class="PageSubNav">
<li>Subpage 1</li>
<li>Subpage 2</li>
<li>Subpage 3</li>
<li>Subpage 4</li>
<li>Subpage 5</li>
<li>Subpage 6</li>
</ul>
</li>
<li id="GalleryLink">Gallery</li>
<li id="VideoLink">Video</li>
<li id="ContactLink">Contact</li>
</ul>
</nav>
<article class="ContentText">
<p>text</p>
<p>dummy text</p>
<p>Dummy text, dummy text, dummy text, dummy text, <strong>3 Paragraphs, Roughly 209 Words</strong></p>
</article>
CSS
#ServicesLink:hover .ContentText p {
color: rgba(255,255,255,0.5);
}
I'm having trouble in thinking this one through, here's what I have but to no avail.
The idea being that when one hovers over the "mega services" tab the text contained in the article section fades.
You can do this by declaring opacity:0 for mega services (I suppose id ServicesLink). Or, if you want it to be smoother you can use jQuery:
$("#id_where_you_hover").hover(function(event){
$("#id_that_you_want_to_fade_out").fadeOut(1000, function () {
$(this).html("here you put something to replace if you want").fadeIn(2000);
});
});
What your CSS is doing is targeting .ContentText p inside #ServicesLink -- which doesn't exist.
Your .ContentText is out of the CSS scope. There is no way in vanilla CSS to target selectors outside of it's parent.
JavaScript is your only valuable solution to scope elements on your page.
What is the aim or purpose of this, so we could maybe give you a better answer?
UPDATE: Updated answer.
In JS, you can do the following:
var hoverEl = $('#ServicesLink');
var targetEl = $('.ContentText p');
hoverEl.on('mouseenter', function() {
targetEl.css({'color': 'rgba(255, 255, 255, 0.5)'});
});
hoverEl.on('mouseleave', function() {
targetEl.css({'color': 'rgba(255, 255, 255, 1)'});
});
DEMO
You can't do that with CSS currently.
You will have to use JavaScript.
I finally got Bootstrap tabs to work. I was wondering if there's a way to change the behaviour so instead of clicking just hovering the cursor would show the hidden content?
In your $(document).ready or elsewhere ...
put something like this:
$('.nav-tabs > li > a').hover(function() {
$(this).tab('show');
});
You wont have to modify the core bootstrap code.
Additionally you could do this:
$('.nav-tabs > li ').hover(function() {
if ($(this).hasClass('hoverblock'))
return;
else
$(this).find('a').tab('show');
});
$('.nav-tabs > li').find('a').click(function() {
$(this).parent()
.siblings().addClass('hoverblock');
});
Which will prevent hover selection after the first time a user clicks a tab.
It's better to bind a handler on the document object so it will work with dynamically generated tabs too:
$(document).on('mouseenter', '[data-toggle="tab"]', function () {
$(this).tab('show');
});
Also there is a small Bootstrap plugin which automatically activates tabs on hover: https://github.com/tonystar/bootstrap-hover-tabs.
The complete HTML using this plugin is:
body { padding: 2rem; }
.tab-content { margin-top: 1.5rem; }
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Nav pills -->
<ul class="nav nav-pills">
<li class="active">Tab 1</li>
<li>Tab 2</li>
<li>Tab 3</li>
<li>Tab 4</li>
</ul>
<!-- Tab panes -->
<div class="tab-content well">
<div class="tab-pane active" id="tab-1">Content 1</div>
<div class="tab-pane" id="tab-2">Content 2</div>
<div class="tab-pane" id="tab-3">Content 3</div>
<div class="tab-pane" id="tab-4">Content 4</div>
</div>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="//cdn.rawgit.com/tonystar/bootstrap-hover-tabs/v3.1.1/bootstrap-hover-tabs.js"></script>
JavaScript:
In your $(document).ready or elsewhere ...
put something like this:
$('.nav-tabs[data-toggle="tab-hover"] > li > a').hover( function(){
$(this).tab('show');
});
HTML:
<ul class="nav nav-tabs" data-toggle="tab-hover">
....
</ul>
Modify bootstrap.js (or boostrap-tab.js if you aren't using the full bootstrap.js file)
Where you see:
/* TAB DATA-API
* ============ */
$(function () {
$('body').on('click.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) {
e.preventDefault()
$(this).tab('show')
})
})
change the 'click.tab.data-api' to 'hover.tab.data-api'
Note: I tested this with Bootstrap v2.0.2
Right from my production code. Orthogonal, unobtrusive, can "unclick" any user interface. Selector can be modified to match many kinds of clickables.
$(document).on('mouseover','.js-mouseover-to-click',function (event) {
$(event.target).trigger('click');
});
i hope it is nice solution
(function ($) {
$(function () {
$(document).off('click.bs.tab.data-api', '[data-hover="tab"]');
$(document).on('mouseenter.bs.tab.data-api', '[data-toggle="tab"], [data-hover="tab"]', function () {
$(this).tab('show');
});
});
})(jQuery);