Question

I got a pseudo-element that marks the user's current choice in a navigation bar. It's a small upward triangle, an icon font from Font-Awesome. here's a jsFiddle DEMO of it (you need to stretch the result panel so everything will be lined).

enter image description here

.subnav > ul > li.active > a:after {
  position: relative;
  text-align: center;
  font-family: FontAwesome;
  top: 25px;
  right: 50%;
  content: "\f0de";
  color: #c1c1c1;
}

I've added some basic jQuery function that switches the .active class, and I'm wondering if there's a way to animate the transition of the pseudo element so it'll move horizontally to the new position.

I know pseudo-elements transition are a thing, but searching and googling around I couldn't find anything similar to what I'm looking for. Is this even possible?

Was it helpful?

Solution

In this solution I used the :target pseudo class to switch states, but I recommend you stick with the jQuery function that switches the .active class.

FIDDLE

Markup

<div class="page" id="one">page one</div>
<div class="page" id="two">page two</div>
<div class="page" id="three">page three</div>

<div class="top">
    <div class="arrow"></div>
    <ul>
        <li><a href="#one">One</a></li>
        <li><a href="#two">Two</a></li>
        <li><a href="#three">Three</a></li>
    </ul>
</div>

CSS

.top
{
    background: #eee;   
    position:relative;
    overflow: hidden;  
}
.arrow
{
    border-bottom: 1px solid #c2c2c2;
    height: 50px;

}
.arrow:before
{
    content: '';
    display: block;
    width: 14px;
    height: 14px;
    border: 1px solid #c2c2c2;
    border-radius: 3px;
    position:absolute;
    bottom:-9px;
    left: 30px;
    background: #fff;
    -webkit-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    -o-transform: rotate(45deg); 
    transform: rotate(45deg);

    -webkit-transition: left, 0.5s;
    -moz-transition: left, 0.5s;
    -o-transition: left, 0.5s;
    transition: left, 0.5s;
}
ul
{
    position: absolute;
    top: 0;
    list-style: none;
    padding-left: 20px;
    margin-top: 15px;
}
li
{
    display: inline-block;
    text-decoration: none;
    font-size: 18px;
    color: #676767;
    margin-right: 40px;
}
.page
{
    width: 100%;
    height: 200px;
    position: absolute;
    top: 80px;
    opacity: 0;
    background: yellow;
    -webkit-transition: opacity, 0.5s;
    -moz-transition: opacity, 0.5s;
    -o-transition: opacity, 0.5s;
    transition: opacity, 0.5s;
}
.page:target
{
    opacity: 1;
}

#two
{
    background: pink;
}
#three
{
    background: brown;
}
#one:target ~ .top .arrow:before
{
    left: 30px;
}
#two:target ~ .top .arrow:before
{
    left: 105px;
}
#three:target ~ .top .arrow:before
{
    left: 189px;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top