2017-01-17 2 views
0

Когда нажата кнопка/ссылка, я хотел бы выделить ее, затухая/растягивая (и затем) новый цвет фона.Как добавить определенную анимацию выделения при нажатии кнопки/ссылки?

В качестве примера: Если вы перейдете в Историю Chrome и нажмите ссылку «История Chrome», «Вкладки с других устройств» или «Очистить данные просмотра», он показывает этот эффект.

Как бы я это сделал?

Thank you.

ответ

1

На самом деле google использует бумажную пульсацию для визуального эффекта, который могут использовать другие бумажные элементы для имитации эффекта ряби, исходящего из точки контакта. Эффект может быть визуализирован как концентрический круг с движением.

Бумажная рябь прислушивается к событиям «mousedown» и «mouseup», поэтому при нажатии на нее будет отображаться эффект пульсации. Вы также можете победить поведение по умолчанию и вручную маршрутизировать действия «вниз» и «вверх» на элемент рябь. Обратите внимание, что важно, если вы вызове downAction(), вам нужно будет вызвать upAction(), чтобы волна бумаги закончила цикл анимации.

Обратите внимание: важно, чтобы родительский контейнер пульсации был относительным положением, иначе пульсация будет выходить за пределы желаемого контейнера.

REFERENCE

//jQuery time 
 
    var parent, ink, d, x, y; 
 
    $("ul li a").click(function(e){ 
 
    \t parent = $(this).parent(); 
 
    \t //create .ink element if it doesn't exist 
 
    \t if(parent.find(".ink").length == 0) 
 
    \t \t parent.prepend("<span class='ink'></span>"); 
 
    \t \t 
 
    \t ink = parent.find(".ink"); 
 
    \t //incase of quick double clicks stop the previous animation 
 
    \t ink.removeClass("animate"); 
 
    \t 
 
    \t //set size of .ink 
 
    \t if(!ink.height() && !ink.width()) 
 
    \t { 
 
    \t \t //use parent's width or height whichever is larger for the diameter to make a circle which can cover the entire element. 
 
    \t \t d = Math.max(parent.outerWidth(), parent.outerHeight()); 
 
    \t \t ink.css({height: d, width: d}); 
 
    \t } 
 
    \t 
 
    \t //get click coordinates 
 
    \t //logic = click coordinates relative to page - parent's position relative to page - half of self height/width to make it controllable from the center; 
 
    \t x = e.pageX - parent.offset().left - ink.width()/2; 
 
    \t y = e.pageY - parent.offset().top - ink.height()/2; 
 
    \t 
 
    \t //set the position and add class .animate 
 
    \t ink.css({top: y+'px', left: x+'px'}).addClass("animate"); 
 
    })
/*custom fonts - Bitter, Montserrat*/ 
 
    @import url('http://fonts.googleapis.com/css?family=Montserrat|Bitter'); 
 
    /*basic reset*/ 
 
    * {margin: 0; padding: 0;} 
 
    body { 
 
    \t background-color:black; 
 
    \t background-size: cover; 
 
    } 
 
    
 
    h1 { 
 
    \t font: normal 32px/32px Bitter; color: white; 
 
    \t text-align: center; padding: 85px 100px; 
 
    } 
 
    
 
    /*nav styles*/ 
 
    ul { 
 
    \t background: white; border-top: 6px solid hsl(180, 40%, 60%); 
 
    \t width: 200px; margin: 0 auto; 
 
    } 
 
    ul li { 
 
    \t list-style-type: none; 
 
    \t /*relative positioning for list items along with overflow hidden to contain the overflowing ripple*/ 
 
    \t position: relative; 
 
    \t overflow: hidden; 
 
    } 
 
    ul li a { 
 
    \t font: normal 14px/28px Montserrat; color: hsl(180, 40%, 40%); 
 
    \t display: block; padding: 10px 15px; 
 
    \t text-decoration: none; 
 
    \t cursor: pointer; /*since the links are dummy without href values*/ 
 
    \t /*prevent text selection*/ 
 
    \t user-select: none; 
 
    \t /*static positioned elements appear behind absolutely positioned siblings(.ink in this case) hence we will make the links relatively positioned to bring them above .ink*/ 
 
    \t position: relative; 
 
    } 
 
    
 
    /*.ink styles - the elements which will create the ripple effect. The size and position of these elements will be set by the JS code. Initially these elements will be scaled down to 0% and later animated to large fading circles on user click.*/ 
 
    .ink { 
 
    \t display: block; position: absolute; 
 
    \t background: hsl(180, 40%, 80%); 
 
    \t border-radius: 100%; 
 
    \t transform: scale(0); 
 
    } 
 
    /*animation effect*/ 
 
    .ink.animate {animation: ripple 0.65s linear;} 
 
    @keyframes ripple { 
 
    \t /*scale the element to 250% to safely cover the entire link and fade it out*/ 
 
    \t 100% {opacity: 0; transform: scale(2.5);} 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<h1>Ripple Click Effect</h1> 
 
    <ul> 
 
    \t <li><a>Dashboard</a></li> 
 
    \t <li><a>My Account</a></li> 
 
    \t <li><a>Direct Messages</a></li> 
 
    \t <li><a>Chat Rooms</a></li> 
 
    \t <li><a>Settings</a></li> 
 
    \t <li><a>Logout</a></li> 
 
    </ul>

WORKING FIDDLE