1

I have this code snippet and wanted to know if there is any possibility to modify it, in order to obtain after hover translation, keeping in place or moving a few more pixels to the right on click event, until another menu botton will be clicked.

// mynewmenu implementation
$('nav ul li').mouseover(function(e){
 //Set the aesthetics (similar to :hover)
 $('nav ul li').removeClass('hovered');
 $(this).addClass('hovered');
});


var pageSize = 4, 
    $links = $(".pagedMenu li"), 
    count = $links.length, 
    numPages = Math.ceil(count / pageSize), 
    curPage = 1
;

showPage(curPage);

function showPage(whichPage) {
    var previousLinks = (whichPage - 1) * pageSize, 
        nextLinks = (previousLinks + pageSize);
    $links.show();
    $links.slice(0, previousLinks).hide();
    $links.slice(nextLinks).hide();
    showPrevNext();
}

function showPrevNext() {
    if ((numPages > 0) && (curPage < numPages)) {
        $("#nextPage").removeClass('hidden'); 
        $("#msg").text("(" + curPage + " of " + numPages + ")");
    } else { 
        $("#nextPage").addClass('hidden'); 
    }
    if ((numPages > 0) && (curPage > 1)) {
        $("#prevPage").removeClass('hidden'); 
        $("#msg").text("(" + curPage + " of " + numPages + ")");
    } else { 
        $("#prevPage").addClass('hidden'); 
    }
}

$("#nextPage").on("click", function() {
    showPage(++curPage);
});
$("#prevPage").on("click", function() {
    showPage(--curPage);
});
.hidden {
    display: none;
}

body {
    font: normal 1.0em Arial, sans-serif;


}


nav.pagedMenu {
    color: red;
    font-size: 2.0em;
    line-height: 1.0em;
    width: 8em;
    position: fixed; 
    top: 50px;
}

nav.pagedMenu ul {

    list-style: none;
    margin: 0;
    padding: 0;
}

nav.pagedMenu ul li {
    height: 1.0em;
    padding: 0.15em;
    position: relative;
    border-top-right-radius: 0em;
    border-bottom-right-radius: 0em;
    -webkit-transition: 
    -webkit-transform 220ms, background-color 200ms, color 500ms;
    transition: transform 220ms, background-color 200ms, color 500ms;
}


nav.pagedMenu ul li.hovered {
    -webkit-transform: translateX(1.5em);
    transform: translateX(1.5em);
}
nav ul li:hover a {
    transition: color, 1200ms;
    color: red;
}
nav.pagedMenu ul li span {
    display:block;
    font-family: Arial;
    position: absolute;
    font-size:1em;
    line-height: 1.25em;
    height:1.0em;
    top:0; bottom:0;
    margin:auto;
    right: 0.01em;
    color: #F8F6FF;

}

a {
    color: gold;
    transition: color, 1200ms;
    text-decoration: none;
}

#pagination, #prevPage, #nextPage {
    font-size: 1.0em;
    color: gold;    
    line-height: 1.0em;
    padding-top: 250px;
    padding-left: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class="pagedMenu">
   <ul style="font-size: 28px;">
     <li class="" style="margin-bottom: 5px;"><a href="#">Link 1</a></li>
     <li class="" style="margin-bottom: 5px;"><a href="#">Link 2</a></li>
     <li class="" style="margin-bottom: 5px;"><a href="#">Link 3</a></li>
     <li class="" style="margin-bottom: 5px;"><a href="#">Link 4</a></li>
     <li class="" style="margin-bottom: 5px;"><a href="#">Link 5</a></li>
     <li class="" style="margin-bottom: 5px;"><a href="#">Link 6</a></li>
     <li class="" style="margin-bottom: 5px;"><a href="#">Link 7</a></li>
     <li class="" style="margin-bottom: 5px;"><a href="#">Link 8</a></li>
     <li class="" style="margin-bottom: 5px;"><a href="#">Link 9</a></li>
     <li class="" style="margin-bottom: 5px;"><a href="#">Link 10</a></li>
     <li class="" style="margin-bottom: 5px;"><a href="#">Link 11</a></li>
     <li class="" style="margin-bottom: 5px;"><a href="#">Link 12</a></li>
  </ul>
</nav>

<div id="pagination">
    <a href="#" id="prevPage" class="hidden">Previous</a>&nbsp;&nbsp;
    <a href="#" id="nextPage" class="hidden">Next</a>
    <span id="msg"></span>
</div>

A live example here.

typo_
  • 11
  • 2
  • 15
  • 37
  • 1
    if i understand your question you want on click the link should be translated am i right? – CY5 Sep 20 '15 at 18:54
  • if that is the case replace mouseover with click event in jquery https://jsfiddle.net/kjhtswp9/ – CY5 Sep 20 '15 at 18:56
  • Indeed, should be translated on click beyond the already translated position. (you could check the [live example](http://goo.gl/K9s3vQ) clicking on *works*, *books* etc) thanks – typo_ Sep 20 '15 at 20:49

1 Answers1

2

after hover translation, keeping in place or moving a few more pixels to the right on click event, until another menu botton will be clicked.

Added .toggleClass(), .hasClass() , .not() , .siblings() , .hover() to jsfiddle https://jsfiddle.net/kjhtswp9/ originally created by @CY5 , maintaining hover translation effect

$(function () {
    $('nav ul li').click(function (e) {
        //Set the aesthetics (similar to :hover)
        $('nav ul li')
        .not(".clicked").removeClass('hovered')
        .filter(this).addClass("clicked hovered")
        .siblings().toggleClass("clicked hovered", false);
    }).hover(function () {
        $(this).addClass("hovered")
    }, function () {
        $(this).not(".clicked").removeClass("hovered")
    });

    var pageSize = 4,
        $links = $(".pagedMenu li"),
        count = $links.length,
        numPages = Math.ceil(count / pageSize),
        curPage = 1;

    showPage(curPage);

    function showPage(whichPage) {
        var previousLinks = (whichPage - 1) * pageSize,
            nextLinks = (previousLinks + pageSize);
        $links.show();
        $links.slice(0, previousLinks).hide();
        $links.slice(nextLinks).hide();
        showPrevNext();
    }

    function showPrevNext() {
        if ((numPages > 0) && (curPage < numPages)) {
            $("#nextPage").removeClass('hidden');
            $("#msg").text("(" + curPage + " of " + numPages + ")");
        } else {
            $("#nextPage").addClass('hidden');
        }
        if ((numPages > 0) && (curPage > 1)) {
            $("#prevPage").removeClass('hidden');
            $("#msg").text("(" + curPage + " of " + numPages + ")");
        } else {
            $("#prevPage").addClass('hidden');
        }
    }

    $("#nextPage").on("click", function () {
        showPage(++curPage);
    });
    $("#prevPage").on("click", function () {
        showPage(--curPage);
    });

});
.hidden {
  display: none;
}
body {
  font: normal 1.0em Arial, sans-serif;
}
nav.pagedMenu {
  color: red;
  font-size: 2.0em;
  line-height: 1.0em;
  width: 8em;
  position: fixed;
  top: 50px;
}
nav.pagedMenu ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
nav.pagedMenu ul li {
  height: 1.0em;
  padding: 0.15em;
  position: relative;
  border-top-right-radius: 0em;
  border-bottom-right-radius: 0em;
  -webkit-transition: -webkit-transform 220ms, background-color 200ms, color 500ms;
  transition: transform 220ms, background-color 200ms, color 500ms;
}
nav.pagedMenu ul li.hovered {
  -webkit-transform: translateX(1.5em);
  transform: translateX(1.5em);
}
nav ul li:hover a {
  transition: color, 1200ms;
  color: red;
}
nav.pagedMenu ul li span {
  display: block;
  font-family: Arial;
  position: absolute;
  font-size: 1em;
  line-height: 1.25em;
  height: 1.0em;
  top: 0;
  bottom: 0;
  margin: auto;
  right: 0.01em;
  color: #F8F6FF;
}
a {
  color: gold;
  transition: color, 1200ms;
  text-decoration: none;
}
#pagination,
#prevPage,
#nextPage {
  font-size: 1.0em;
  color: gold;
  line-height: 1.0em;
  padding-top: 250px;
  padding-left: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class="pagedMenu">
  <ul style="font-size: 28px;">
    <li class="" style="margin-bottom: 5px;"><a href="#">Link 1</a>
    </li>
    <li class="" style="margin-bottom: 5px;"><a href="#">Link 2</a>
    </li>
    <li class="" style="margin-bottom: 5px;"><a href="#">Link 3</a>
    </li>
    <li class="" style="margin-bottom: 5px;"><a href="#">Link 4</a>
    </li>
    <li class="" style="margin-bottom: 5px;"><a href="#">Link 5</a>
    </li>
    <li class="" style="margin-bottom: 5px;"><a href="#">Link 6</a>
    </li>
    <li class="" style="margin-bottom: 5px;"><a href="#">Link 7</a>
    </li>
    <li class="" style="margin-bottom: 5px;"><a href="#">Link 8</a>
    </li>
    <li class="" style="margin-bottom: 5px;"><a href="#">Link 9</a>
    </li>
    <li class="" style="margin-bottom: 5px;"><a href="#">Link 10</a>
    </li>
    <li class="" style="margin-bottom: 5px;"><a href="#">Link 11</a>
    </li>
    <li class="" style="margin-bottom: 5px;"><a href="#">Link 12</a>
    </li>
  </ul>
</nav>

<div id="pagination">
  <a href="#" id="prevPage" class="hidden">Previous</a>&nbsp;&nbsp;
  <a href="#" id="nextPage" class="hidden">Next</a>
  <span id="msg"></span>
</div>

jsfiddle http://jsfiddle.net/kjhtswp9/3

guest271314
  • 1
  • 15
  • 104
  • 177
  • Almost there, but next/previous buttons are missing.thanks – typo_ Sep 20 '15 at 20:51
  • @typo_78 Did not address next/previous buttons at `js` . What is expected effect of clicking next/previous buttons ? – guest271314 Sep 20 '15 at 20:55
  • I've updated your jsfiddle [here](https://jsfiddle.net/bpu95yp5/) to see the whole picture but unfortunately, in wordpress doesn't remain fixed (or translated not your code case) on the page that is opened please have a look [here](http://goo.gl/xGBMtI); next/prev buttons are there in order to keep a predefined menu list (like 4 items / section for example and next menu links on other section) – typo_ Sep 20 '15 at 21:06
  • In other words, the clicked menu should remain in place or should be translated on click beyond the already translated position. (you could check the [live example](http://goo.gl/K9s3vQ) clicking on *works*, *books* etc) – typo_ Sep 20 '15 at 21:10
  • @typo_78 Not certain interpret comment correctly ? next/previous buttons change hovered `li` element to previous of next `li` element ? – guest271314 Sep 20 '15 at 21:14
  • nope, just split the menu in a few pieces when there are more links in order to keep a defined number of items/section. If I understand your question `var pageSize = 4`, will define the number of menu items / section. – typo_ Sep 20 '15 at 21:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90177/discussion-between-typo-78-and-guest271314). – typo_ Sep 20 '15 at 21:21
  • not really, the clicked item doesn't remain there fixed (or translated beyond the already translated position) on the linked page - in other words for example if I click to Project 1 menu link, the button should remain there translated (or translated beyond) even on the redirected page; please [have a look](http://goo.gl/xGBMtI). – typo_ Sep 20 '15 at 21:58
  • 1
    @typo_78 https://jsfiddle.net/kjhtswp9/3/ should maintain link until another link clicked . _"for example if I click to Project 1 menu link, the button should remain there translated (or translated beyond) **even on the redirected page**"_ "even on the redirected page" portion not appear at description of Question at original post : _"any possibility to modify it, in order to obtain after hover translation, keeping in place or moving a few more pixels to the right on click event, until another menu botton will be clicked."_ ? See http://stackoverflow.com/q/29986657/ – guest271314 Sep 20 '15 at 22:31
  • U're right, I thought it's obvious giving the live example site. Unfortunately, I am not able to implement that solution because of lack of background knowledge in js/jQuery. I'll update the question. Thank you for your effort. – typo_ Sep 21 '15 at 04:26
  • @typo_78 _"I'll update the question"_ ? Original Question at OP _"I have this code snippet and wanted to know if there is any possibility to modify it, in order to obtain after hover translation, keeping in place or moving a few more pixels to the right on click event, until another menu botton will be clicked."_ not resolved ? Could ask another Question as to _" if I click to Project 1 menu link, the button should remain there translated (or translated beyond) even on the redirected page"_ ? – guest271314 Sep 21 '15 at 04:30
  • @typo_78 Note, did not address next/previous buttons at `js` at post , jsfiddle – guest271314 Sep 21 '15 at 04:58
  • it seems to work including next/prew buttons. Not so sure if I understand your note :) – typo_ Sep 21 '15 at 05:03
  • @typo_78 Ok. next/previous buttons not appear visible at stacksnippets , here – guest271314 Sep 21 '15 at 05:06
  • in full screen yes, if that was your concern :) in my question/ your edited answer. – typo_ Sep 21 '15 at 05:10
  • True, I didn't test it yet on page reload, hope that remaining unchanged will be fine. I don't see any reason to become scrambled if the code will be modified above `var pageSize = 4` line; – typo_ Sep 21 '15 at 05:18
  • 1
    @typo_78 Removed `.not(this)` following `.siblings()` ; not needed to return same results – guest271314 Sep 21 '15 at 05:34
  • Thanks, updated also in my [early question](http://stackoverflow.com/questions/32687806/custom-animation-menu-on-page-reload). – typo_ Sep 21 '15 at 07:05