Use mouseenter & mouseleave event listeners to fadeIn & fadeOut an element on the page
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
I'd like to see if it's possible to make this code more DRY.
$( '#menu-primary-navigation' ).on(
mouseenter: function()
pageScreen.fadeIn();
,
mouseleave: function()
pageScreen.fadeOut();
);
javascript jquery
add a comment |Â
up vote
0
down vote
favorite
I'd like to see if it's possible to make this code more DRY.
$( '#menu-primary-navigation' ).on(
mouseenter: function()
pageScreen.fadeIn();
,
mouseleave: function()
pageScreen.fadeOut();
);
javascript jquery
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'd like to see if it's possible to make this code more DRY.
$( '#menu-primary-navigation' ).on(
mouseenter: function()
pageScreen.fadeIn();
,
mouseleave: function()
pageScreen.fadeOut();
);
javascript jquery
I'd like to see if it's possible to make this code more DRY.
$( '#menu-primary-navigation' ).on(
mouseenter: function()
pageScreen.fadeIn();
,
mouseleave: function()
pageScreen.fadeOut();
);
javascript jquery
edited Jul 11 at 22:56
Jamalâ¦
30.1k11114225
30.1k11114225
asked Jul 11 at 22:45
thenomadicmann
11
11
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
I must admit to a bit of a chuckle when reading this question as there are only 8 lines of code which is already fairly DRY.
As it stands alone one change you could make to DRY it out a little is use function shorthand for the mouse events
$( '#menu-primary-navigation' ).on(
mouseenter() pageScreen.fadeIn() ,
mouseleave() pageScreen.fadeOut()
);
You could go ultra dry and unreadable, using an array to hold only the parts of the names that change, and Array.reduce
to construct the object required for $().on
.
$( '#menu-primary-navigation' ).on(
[["enter", "in"], ["leave", "out"]].reduce((obj, n) =>
(obj["mouse" + n[0]] = () => pageScreen["fade" + n[1]](), obj),
)
)
That would be the worst way to dry out the code
Or you could reference the functions directly, BUT I am not familiar enough with JQuery to know if you would need to bind the functions to pageScreen
or not. That would however add repeating code, defeating the exercise
// may work
$( '#menu-primary-navigation' ).on(
mouseenter : pageScreen.fadeIn,
mouseleave : pageScreen.fadeOut
);
// or if not bind the functions to pageScreen
$( '#menu-primary-navigation' ).on(
mouseenter : pageScreen.fadeIn.bind(pageScreen),
mouseleave : pageScreen.fadeOut.bind(pageScreen)
);
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I must admit to a bit of a chuckle when reading this question as there are only 8 lines of code which is already fairly DRY.
As it stands alone one change you could make to DRY it out a little is use function shorthand for the mouse events
$( '#menu-primary-navigation' ).on(
mouseenter() pageScreen.fadeIn() ,
mouseleave() pageScreen.fadeOut()
);
You could go ultra dry and unreadable, using an array to hold only the parts of the names that change, and Array.reduce
to construct the object required for $().on
.
$( '#menu-primary-navigation' ).on(
[["enter", "in"], ["leave", "out"]].reduce((obj, n) =>
(obj["mouse" + n[0]] = () => pageScreen["fade" + n[1]](), obj),
)
)
That would be the worst way to dry out the code
Or you could reference the functions directly, BUT I am not familiar enough with JQuery to know if you would need to bind the functions to pageScreen
or not. That would however add repeating code, defeating the exercise
// may work
$( '#menu-primary-navigation' ).on(
mouseenter : pageScreen.fadeIn,
mouseleave : pageScreen.fadeOut
);
// or if not bind the functions to pageScreen
$( '#menu-primary-navigation' ).on(
mouseenter : pageScreen.fadeIn.bind(pageScreen),
mouseleave : pageScreen.fadeOut.bind(pageScreen)
);
add a comment |Â
up vote
0
down vote
I must admit to a bit of a chuckle when reading this question as there are only 8 lines of code which is already fairly DRY.
As it stands alone one change you could make to DRY it out a little is use function shorthand for the mouse events
$( '#menu-primary-navigation' ).on(
mouseenter() pageScreen.fadeIn() ,
mouseleave() pageScreen.fadeOut()
);
You could go ultra dry and unreadable, using an array to hold only the parts of the names that change, and Array.reduce
to construct the object required for $().on
.
$( '#menu-primary-navigation' ).on(
[["enter", "in"], ["leave", "out"]].reduce((obj, n) =>
(obj["mouse" + n[0]] = () => pageScreen["fade" + n[1]](), obj),
)
)
That would be the worst way to dry out the code
Or you could reference the functions directly, BUT I am not familiar enough with JQuery to know if you would need to bind the functions to pageScreen
or not. That would however add repeating code, defeating the exercise
// may work
$( '#menu-primary-navigation' ).on(
mouseenter : pageScreen.fadeIn,
mouseleave : pageScreen.fadeOut
);
// or if not bind the functions to pageScreen
$( '#menu-primary-navigation' ).on(
mouseenter : pageScreen.fadeIn.bind(pageScreen),
mouseleave : pageScreen.fadeOut.bind(pageScreen)
);
add a comment |Â
up vote
0
down vote
up vote
0
down vote
I must admit to a bit of a chuckle when reading this question as there are only 8 lines of code which is already fairly DRY.
As it stands alone one change you could make to DRY it out a little is use function shorthand for the mouse events
$( '#menu-primary-navigation' ).on(
mouseenter() pageScreen.fadeIn() ,
mouseleave() pageScreen.fadeOut()
);
You could go ultra dry and unreadable, using an array to hold only the parts of the names that change, and Array.reduce
to construct the object required for $().on
.
$( '#menu-primary-navigation' ).on(
[["enter", "in"], ["leave", "out"]].reduce((obj, n) =>
(obj["mouse" + n[0]] = () => pageScreen["fade" + n[1]](), obj),
)
)
That would be the worst way to dry out the code
Or you could reference the functions directly, BUT I am not familiar enough with JQuery to know if you would need to bind the functions to pageScreen
or not. That would however add repeating code, defeating the exercise
// may work
$( '#menu-primary-navigation' ).on(
mouseenter : pageScreen.fadeIn,
mouseleave : pageScreen.fadeOut
);
// or if not bind the functions to pageScreen
$( '#menu-primary-navigation' ).on(
mouseenter : pageScreen.fadeIn.bind(pageScreen),
mouseleave : pageScreen.fadeOut.bind(pageScreen)
);
I must admit to a bit of a chuckle when reading this question as there are only 8 lines of code which is already fairly DRY.
As it stands alone one change you could make to DRY it out a little is use function shorthand for the mouse events
$( '#menu-primary-navigation' ).on(
mouseenter() pageScreen.fadeIn() ,
mouseleave() pageScreen.fadeOut()
);
You could go ultra dry and unreadable, using an array to hold only the parts of the names that change, and Array.reduce
to construct the object required for $().on
.
$( '#menu-primary-navigation' ).on(
[["enter", "in"], ["leave", "out"]].reduce((obj, n) =>
(obj["mouse" + n[0]] = () => pageScreen["fade" + n[1]](), obj),
)
)
That would be the worst way to dry out the code
Or you could reference the functions directly, BUT I am not familiar enough with JQuery to know if you would need to bind the functions to pageScreen
or not. That would however add repeating code, defeating the exercise
// may work
$( '#menu-primary-navigation' ).on(
mouseenter : pageScreen.fadeIn,
mouseleave : pageScreen.fadeOut
);
// or if not bind the functions to pageScreen
$( '#menu-primary-navigation' ).on(
mouseenter : pageScreen.fadeIn.bind(pageScreen),
mouseleave : pageScreen.fadeOut.bind(pageScreen)
);
answered Jul 12 at 11:47
Blindman67
5,2381320
5,2381320
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f198331%2fuse-mouseenter-mouseleave-event-listeners-to-fadein-fadeout-an-element-on-th%23new-answer', 'question_page');
);
Post as a guest
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
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
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