How to add and remove class to menu items on mouseover
I want to add and remove a 'hover-open' class to the menu item when mouseover on that item. The code I tried is adding the class to all the menu items when mouseovered on a single item.
menu-bar.component.html
<ul>
<li (mouseover)="changeStyle()" (mouseout)="changeStyle()" [className]="hovered ? 'hover-open nav-item':'nav-item'"><a href="#">Link 1</a></li>
<li (mouseover)="changeStyle()" (mouseout)="changeStyle()" [className]="hovered ? 'hover-open nav-item':'nav-item'"><a href="#">Link 2</a></li>
<li (mouseover)="changeStyle()" (mouseout)="changeStyle()" [className]="hovered ? 'hover-open nav-item':'nav-item'"><a href="#">Link 3</a></li>
<li (mouseover)="changeStyle()" (mouseout)="changeStyle()" [className]="hovered ? 'hover-open nav-item':'nav-item'"><a href="#">Link 4</a></li>
</ul>
menu-bar.component.ts
import Component, OnInit from '@angular/core';
@Component(
selector: 'app-menu-bar',
templateUrl: './menu-bar.component.html'
)
export class MenuBarComponent implements OnInit
constructor()
ngOnInit()
hovered = false;
changeStyle($event)
this.hovered = !this.hovered;
javascript angular typescript
add a comment |
I want to add and remove a 'hover-open' class to the menu item when mouseover on that item. The code I tried is adding the class to all the menu items when mouseovered on a single item.
menu-bar.component.html
<ul>
<li (mouseover)="changeStyle()" (mouseout)="changeStyle()" [className]="hovered ? 'hover-open nav-item':'nav-item'"><a href="#">Link 1</a></li>
<li (mouseover)="changeStyle()" (mouseout)="changeStyle()" [className]="hovered ? 'hover-open nav-item':'nav-item'"><a href="#">Link 2</a></li>
<li (mouseover)="changeStyle()" (mouseout)="changeStyle()" [className]="hovered ? 'hover-open nav-item':'nav-item'"><a href="#">Link 3</a></li>
<li (mouseover)="changeStyle()" (mouseout)="changeStyle()" [className]="hovered ? 'hover-open nav-item':'nav-item'"><a href="#">Link 4</a></li>
</ul>
menu-bar.component.ts
import Component, OnInit from '@angular/core';
@Component(
selector: 'app-menu-bar',
templateUrl: './menu-bar.component.html'
)
export class MenuBarComponent implements OnInit
constructor()
ngOnInit()
hovered = false;
changeStyle($event)
this.hovered = !this.hovered;
javascript angular typescript
1
Use css instead of javascipt...
– Ebin Manuval
Nov 13 '18 at 4:11
add a comment |
I want to add and remove a 'hover-open' class to the menu item when mouseover on that item. The code I tried is adding the class to all the menu items when mouseovered on a single item.
menu-bar.component.html
<ul>
<li (mouseover)="changeStyle()" (mouseout)="changeStyle()" [className]="hovered ? 'hover-open nav-item':'nav-item'"><a href="#">Link 1</a></li>
<li (mouseover)="changeStyle()" (mouseout)="changeStyle()" [className]="hovered ? 'hover-open nav-item':'nav-item'"><a href="#">Link 2</a></li>
<li (mouseover)="changeStyle()" (mouseout)="changeStyle()" [className]="hovered ? 'hover-open nav-item':'nav-item'"><a href="#">Link 3</a></li>
<li (mouseover)="changeStyle()" (mouseout)="changeStyle()" [className]="hovered ? 'hover-open nav-item':'nav-item'"><a href="#">Link 4</a></li>
</ul>
menu-bar.component.ts
import Component, OnInit from '@angular/core';
@Component(
selector: 'app-menu-bar',
templateUrl: './menu-bar.component.html'
)
export class MenuBarComponent implements OnInit
constructor()
ngOnInit()
hovered = false;
changeStyle($event)
this.hovered = !this.hovered;
javascript angular typescript
I want to add and remove a 'hover-open' class to the menu item when mouseover on that item. The code I tried is adding the class to all the menu items when mouseovered on a single item.
menu-bar.component.html
<ul>
<li (mouseover)="changeStyle()" (mouseout)="changeStyle()" [className]="hovered ? 'hover-open nav-item':'nav-item'"><a href="#">Link 1</a></li>
<li (mouseover)="changeStyle()" (mouseout)="changeStyle()" [className]="hovered ? 'hover-open nav-item':'nav-item'"><a href="#">Link 2</a></li>
<li (mouseover)="changeStyle()" (mouseout)="changeStyle()" [className]="hovered ? 'hover-open nav-item':'nav-item'"><a href="#">Link 3</a></li>
<li (mouseover)="changeStyle()" (mouseout)="changeStyle()" [className]="hovered ? 'hover-open nav-item':'nav-item'"><a href="#">Link 4</a></li>
</ul>
menu-bar.component.ts
import Component, OnInit from '@angular/core';
@Component(
selector: 'app-menu-bar',
templateUrl: './menu-bar.component.html'
)
export class MenuBarComponent implements OnInit
constructor()
ngOnInit()
hovered = false;
changeStyle($event)
this.hovered = !this.hovered;
javascript angular typescript
javascript angular typescript
edited Nov 13 '18 at 4:10
Akber Iqbal
1,8622822
1,8622822
asked Nov 13 '18 at 4:07
FebulFebul
839
839
1
Use css instead of javascipt...
– Ebin Manuval
Nov 13 '18 at 4:11
add a comment |
1
Use css instead of javascipt...
– Ebin Manuval
Nov 13 '18 at 4:11
1
1
Use css instead of javascipt...
– Ebin Manuval
Nov 13 '18 at 4:11
Use css instead of javascipt...
– Ebin Manuval
Nov 13 '18 at 4:11
add a comment |
4 Answers
4
active
oldest
votes
use CSS :hover Pseudo-class, and it will work.
css :
li: hover
write down style which you want to apply
add a comment |
You do this using *ngFor
<ul>
<li *ngFor="let menu of menuItems" (mouseover)="changeStyle(menu)" (mouseout)="changeStyle(menu)" [className]="menu.hovered ? 'hover-open nav-item':'nav-item'"><a href="#">menu.name</a></li>
</ul>
menu-bar.component.ts
import Component, OnInit from '@angular/core';
@Component(
selector: 'app-menu-bar',
templateUrl: './menu-bar.component.html'
)
export class MenuBarComponent implements OnInit
menuItems: any = ;
constructor()
this.menuItems = [name: 'link1', name: 'link2']
ngOnInit()
changeStyle(menu)
menu.hovered = !menu.hovered;
add a comment |
As you have multiple elements using the same function you need some identity to identify which nav-item you need to add class.
Note this change:
(mouseover)="changeStyle('link1','in')" [class.hover-open]="hovered === 'link1'" (mouseout)="changeStyle('link1','out')" class="nav-item"
What I changed?
(mouseover)="changeStyle('link1','in')" //passing unique id for each li
(mouseout)="changeStyle('link1','out')" // passing out to remove the class
[class.hover-open]="hovered === 'link1'" //checking if link is link1
then add the class other wise remove
You can do something like this
<ul>
<li id="link1" (mouseover)="changeStyle('link1','in')" [class.hover-open]="hovered ===
'link1'" (mouseout)="changeStyle('link1','out')"
class="nav-item"><a href="#">Link 1</a>
</li>
<li id="link1" (mouseover)="changeStyle('link2','in')"
[class.hover-open]="hovered === 'link2'" (mouseout)="changeStyle('link2','out')"
class="nav-item">
<a href="#">Link 2</a></li>
<li id="link1" (mouseover)="changeStyle('link3','in')" [class.hover-open]="hovered === 'link3'" (mouseout)="changeStyle('link3','out')" class="nav-item"><a href="#">Link 3</a></li>
<li id="link1" (mouseover)="changeStyle('link4','in')"
[class.hover-open]="hovered === 'link4'" (mouseout)="changeStyle('link4','out')"
class="nav-item"><a href="#">Link 4</a></li>
</ul>
export class AppComponent
hovered = '';
changeStyle(linkName, typeOfMove)
if (typeOfMove === 'out')
this.hovered = '';
else
this.hovered = linkName;
Here is the demo of it: https://stackblitz.com/edit/angular-jvk15a
add a comment |
I think this code be done on css if you just used only for changing style.
li // default style for unhovered
li:hover // style when hover
if you want access it by DOM. maybe you could try this one
changeStyle(event)
const klass = event.classList
if (klass.contains("hover-open"))
// remove class here
else // add the class here
If hope this would help.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273656%2fhow-to-add-and-remove-class-to-menu-items-on-mouseover%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
use CSS :hover Pseudo-class, and it will work.
css :
li: hover
write down style which you want to apply
add a comment |
use CSS :hover Pseudo-class, and it will work.
css :
li: hover
write down style which you want to apply
add a comment |
use CSS :hover Pseudo-class, and it will work.
css :
li: hover
write down style which you want to apply
use CSS :hover Pseudo-class, and it will work.
css :
li: hover
write down style which you want to apply
answered Nov 13 '18 at 4:25
Mohit JainMohit Jain
1916
1916
add a comment |
add a comment |
You do this using *ngFor
<ul>
<li *ngFor="let menu of menuItems" (mouseover)="changeStyle(menu)" (mouseout)="changeStyle(menu)" [className]="menu.hovered ? 'hover-open nav-item':'nav-item'"><a href="#">menu.name</a></li>
</ul>
menu-bar.component.ts
import Component, OnInit from '@angular/core';
@Component(
selector: 'app-menu-bar',
templateUrl: './menu-bar.component.html'
)
export class MenuBarComponent implements OnInit
menuItems: any = ;
constructor()
this.menuItems = [name: 'link1', name: 'link2']
ngOnInit()
changeStyle(menu)
menu.hovered = !menu.hovered;
add a comment |
You do this using *ngFor
<ul>
<li *ngFor="let menu of menuItems" (mouseover)="changeStyle(menu)" (mouseout)="changeStyle(menu)" [className]="menu.hovered ? 'hover-open nav-item':'nav-item'"><a href="#">menu.name</a></li>
</ul>
menu-bar.component.ts
import Component, OnInit from '@angular/core';
@Component(
selector: 'app-menu-bar',
templateUrl: './menu-bar.component.html'
)
export class MenuBarComponent implements OnInit
menuItems: any = ;
constructor()
this.menuItems = [name: 'link1', name: 'link2']
ngOnInit()
changeStyle(menu)
menu.hovered = !menu.hovered;
add a comment |
You do this using *ngFor
<ul>
<li *ngFor="let menu of menuItems" (mouseover)="changeStyle(menu)" (mouseout)="changeStyle(menu)" [className]="menu.hovered ? 'hover-open nav-item':'nav-item'"><a href="#">menu.name</a></li>
</ul>
menu-bar.component.ts
import Component, OnInit from '@angular/core';
@Component(
selector: 'app-menu-bar',
templateUrl: './menu-bar.component.html'
)
export class MenuBarComponent implements OnInit
menuItems: any = ;
constructor()
this.menuItems = [name: 'link1', name: 'link2']
ngOnInit()
changeStyle(menu)
menu.hovered = !menu.hovered;
You do this using *ngFor
<ul>
<li *ngFor="let menu of menuItems" (mouseover)="changeStyle(menu)" (mouseout)="changeStyle(menu)" [className]="menu.hovered ? 'hover-open nav-item':'nav-item'"><a href="#">menu.name</a></li>
</ul>
menu-bar.component.ts
import Component, OnInit from '@angular/core';
@Component(
selector: 'app-menu-bar',
templateUrl: './menu-bar.component.html'
)
export class MenuBarComponent implements OnInit
menuItems: any = ;
constructor()
this.menuItems = [name: 'link1', name: 'link2']
ngOnInit()
changeStyle(menu)
menu.hovered = !menu.hovered;
answered Nov 13 '18 at 4:19
Abdul BasitAbdul Basit
18410
18410
add a comment |
add a comment |
As you have multiple elements using the same function you need some identity to identify which nav-item you need to add class.
Note this change:
(mouseover)="changeStyle('link1','in')" [class.hover-open]="hovered === 'link1'" (mouseout)="changeStyle('link1','out')" class="nav-item"
What I changed?
(mouseover)="changeStyle('link1','in')" //passing unique id for each li
(mouseout)="changeStyle('link1','out')" // passing out to remove the class
[class.hover-open]="hovered === 'link1'" //checking if link is link1
then add the class other wise remove
You can do something like this
<ul>
<li id="link1" (mouseover)="changeStyle('link1','in')" [class.hover-open]="hovered ===
'link1'" (mouseout)="changeStyle('link1','out')"
class="nav-item"><a href="#">Link 1</a>
</li>
<li id="link1" (mouseover)="changeStyle('link2','in')"
[class.hover-open]="hovered === 'link2'" (mouseout)="changeStyle('link2','out')"
class="nav-item">
<a href="#">Link 2</a></li>
<li id="link1" (mouseover)="changeStyle('link3','in')" [class.hover-open]="hovered === 'link3'" (mouseout)="changeStyle('link3','out')" class="nav-item"><a href="#">Link 3</a></li>
<li id="link1" (mouseover)="changeStyle('link4','in')"
[class.hover-open]="hovered === 'link4'" (mouseout)="changeStyle('link4','out')"
class="nav-item"><a href="#">Link 4</a></li>
</ul>
export class AppComponent
hovered = '';
changeStyle(linkName, typeOfMove)
if (typeOfMove === 'out')
this.hovered = '';
else
this.hovered = linkName;
Here is the demo of it: https://stackblitz.com/edit/angular-jvk15a
add a comment |
As you have multiple elements using the same function you need some identity to identify which nav-item you need to add class.
Note this change:
(mouseover)="changeStyle('link1','in')" [class.hover-open]="hovered === 'link1'" (mouseout)="changeStyle('link1','out')" class="nav-item"
What I changed?
(mouseover)="changeStyle('link1','in')" //passing unique id for each li
(mouseout)="changeStyle('link1','out')" // passing out to remove the class
[class.hover-open]="hovered === 'link1'" //checking if link is link1
then add the class other wise remove
You can do something like this
<ul>
<li id="link1" (mouseover)="changeStyle('link1','in')" [class.hover-open]="hovered ===
'link1'" (mouseout)="changeStyle('link1','out')"
class="nav-item"><a href="#">Link 1</a>
</li>
<li id="link1" (mouseover)="changeStyle('link2','in')"
[class.hover-open]="hovered === 'link2'" (mouseout)="changeStyle('link2','out')"
class="nav-item">
<a href="#">Link 2</a></li>
<li id="link1" (mouseover)="changeStyle('link3','in')" [class.hover-open]="hovered === 'link3'" (mouseout)="changeStyle('link3','out')" class="nav-item"><a href="#">Link 3</a></li>
<li id="link1" (mouseover)="changeStyle('link4','in')"
[class.hover-open]="hovered === 'link4'" (mouseout)="changeStyle('link4','out')"
class="nav-item"><a href="#">Link 4</a></li>
</ul>
export class AppComponent
hovered = '';
changeStyle(linkName, typeOfMove)
if (typeOfMove === 'out')
this.hovered = '';
else
this.hovered = linkName;
Here is the demo of it: https://stackblitz.com/edit/angular-jvk15a
add a comment |
As you have multiple elements using the same function you need some identity to identify which nav-item you need to add class.
Note this change:
(mouseover)="changeStyle('link1','in')" [class.hover-open]="hovered === 'link1'" (mouseout)="changeStyle('link1','out')" class="nav-item"
What I changed?
(mouseover)="changeStyle('link1','in')" //passing unique id for each li
(mouseout)="changeStyle('link1','out')" // passing out to remove the class
[class.hover-open]="hovered === 'link1'" //checking if link is link1
then add the class other wise remove
You can do something like this
<ul>
<li id="link1" (mouseover)="changeStyle('link1','in')" [class.hover-open]="hovered ===
'link1'" (mouseout)="changeStyle('link1','out')"
class="nav-item"><a href="#">Link 1</a>
</li>
<li id="link1" (mouseover)="changeStyle('link2','in')"
[class.hover-open]="hovered === 'link2'" (mouseout)="changeStyle('link2','out')"
class="nav-item">
<a href="#">Link 2</a></li>
<li id="link1" (mouseover)="changeStyle('link3','in')" [class.hover-open]="hovered === 'link3'" (mouseout)="changeStyle('link3','out')" class="nav-item"><a href="#">Link 3</a></li>
<li id="link1" (mouseover)="changeStyle('link4','in')"
[class.hover-open]="hovered === 'link4'" (mouseout)="changeStyle('link4','out')"
class="nav-item"><a href="#">Link 4</a></li>
</ul>
export class AppComponent
hovered = '';
changeStyle(linkName, typeOfMove)
if (typeOfMove === 'out')
this.hovered = '';
else
this.hovered = linkName;
Here is the demo of it: https://stackblitz.com/edit/angular-jvk15a
As you have multiple elements using the same function you need some identity to identify which nav-item you need to add class.
Note this change:
(mouseover)="changeStyle('link1','in')" [class.hover-open]="hovered === 'link1'" (mouseout)="changeStyle('link1','out')" class="nav-item"
What I changed?
(mouseover)="changeStyle('link1','in')" //passing unique id for each li
(mouseout)="changeStyle('link1','out')" // passing out to remove the class
[class.hover-open]="hovered === 'link1'" //checking if link is link1
then add the class other wise remove
You can do something like this
<ul>
<li id="link1" (mouseover)="changeStyle('link1','in')" [class.hover-open]="hovered ===
'link1'" (mouseout)="changeStyle('link1','out')"
class="nav-item"><a href="#">Link 1</a>
</li>
<li id="link1" (mouseover)="changeStyle('link2','in')"
[class.hover-open]="hovered === 'link2'" (mouseout)="changeStyle('link2','out')"
class="nav-item">
<a href="#">Link 2</a></li>
<li id="link1" (mouseover)="changeStyle('link3','in')" [class.hover-open]="hovered === 'link3'" (mouseout)="changeStyle('link3','out')" class="nav-item"><a href="#">Link 3</a></li>
<li id="link1" (mouseover)="changeStyle('link4','in')"
[class.hover-open]="hovered === 'link4'" (mouseout)="changeStyle('link4','out')"
class="nav-item"><a href="#">Link 4</a></li>
</ul>
export class AppComponent
hovered = '';
changeStyle(linkName, typeOfMove)
if (typeOfMove === 'out')
this.hovered = '';
else
this.hovered = linkName;
Here is the demo of it: https://stackblitz.com/edit/angular-jvk15a
answered Nov 13 '18 at 4:22
Just codeJust code
9,78752966
9,78752966
add a comment |
add a comment |
I think this code be done on css if you just used only for changing style.
li // default style for unhovered
li:hover // style when hover
if you want access it by DOM. maybe you could try this one
changeStyle(event)
const klass = event.classList
if (klass.contains("hover-open"))
// remove class here
else // add the class here
If hope this would help.
add a comment |
I think this code be done on css if you just used only for changing style.
li // default style for unhovered
li:hover // style when hover
if you want access it by DOM. maybe you could try this one
changeStyle(event)
const klass = event.classList
if (klass.contains("hover-open"))
// remove class here
else // add the class here
If hope this would help.
add a comment |
I think this code be done on css if you just used only for changing style.
li // default style for unhovered
li:hover // style when hover
if you want access it by DOM. maybe you could try this one
changeStyle(event)
const klass = event.classList
if (klass.contains("hover-open"))
// remove class here
else // add the class here
If hope this would help.
I think this code be done on css if you just used only for changing style.
li // default style for unhovered
li:hover // style when hover
if you want access it by DOM. maybe you could try this one
changeStyle(event)
const klass = event.classList
if (klass.contains("hover-open"))
// remove class here
else // add the class here
If hope this would help.
answered Nov 13 '18 at 4:27
Agent DroidAgent Droid
916
916
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273656%2fhow-to-add-and-remove-class-to-menu-items-on-mouseover%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Use css instead of javascipt...
– Ebin Manuval
Nov 13 '18 at 4:11