Use mouseenter & mouseleave event listeners to fadeIn & fadeOut an element on the page

The name of the pictureThe name of the pictureThe name of the pictureClash 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();

);






share|improve this question



























    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();

    );






    share|improve this question























      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();

      );






      share|improve this question













      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();

      );








      share|improve this question












      share|improve this question




      share|improve this question








      edited Jul 11 at 22:56









      Jamal♦

      30.1k11114225




      30.1k11114225









      asked Jul 11 at 22:45









      thenomadicmann

      11




      11




















          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)
          );





          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            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: "196"
            ;
            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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            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






























            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)
            );





            share|improve this answer

























              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)
              );





              share|improve this answer























                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)
                );





                share|improve this answer













                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)
                );






                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Jul 12 at 11:47









                Blindman67

                5,2381320




                5,2381320






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    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













































































                    Popular posts from this blog

                    Chat program with C++ and SFML

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    Will my employers contract hold up in court?