CSS涟漪对哈弗的影响?

时间:2018-01-14 19:54:36

标签: javascript jquery css button hover

我有一个按钮,我想要一个略微透明的覆盖物,以便滑过'悬停按钮。

有一种叫做“涟漪效应”的东西。通常可以通过点击按钮来实现。

像这里:https://codepen.io/tomma5o/pen/zwyKya

HTML:

<div class="container">
    <a data-animation="ripple">Click Me</a>
</div>

CSS:

:root {
    /* if u want to change the color of
     * the ripple change this value
    */
    --color-ripple: rgba(255,255,255,0.8);
}

body {
    background: #36353c;
}

.container {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    height: 50px;
    width: 200px;
    margin: auto;
}
*[data-animation="ripple"] {
    position: relative; /*Position relative is required*/
    height: 100%;
    width: 100%;
    display: block;
    outline: none;:root {
    /* if u want to change the color of
     * the ripple change this value
    */
    --color-ripple: rgba(255, 255, 255, 0.8);
}

body {
    background: #36353c;
}

.container {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    height: 50px;
    width: 200px;
    margin: auto;
}
*[data-animation="ripple"] {
    position: relative; /*Position relative is required*/
    height: 100%;
    width: 100%;
    display: block;
    outline: none;
    padding: 20px;
    color: #fff;
    text-transform: uppercase;
    background: linear-gradient(135deg, #e570e7 0%, #79f1fc 100%);
    box-sizing: border-box;
    text-align: center;
    line-height: 14px;
    font-family: roboto, helvetica;
    font-weight: 200;
    letter-spacing: 1px;
    text-decoration: none;
    box-shadow: 0 5px 3px rgba(0, 0, 0, 0.3);
    cursor: pointer;
    /*border-radius: 50px;*/
    -webkit-tap-highlight-color: transparent;
}

*[data-animation="ripple"]:focus {
    outline: none;
}

*[data-animation="ripple"]::selection {
    background: transparent;
    pointer-events: none;
}

    padding: 20px;
    color: #fff;
    text-transform: uppercase;
    background: linear-gradient(135deg, #e570e7 0%,#79f1fc 100%);
    box-sizing: border-box;
    text-align: center;
    line-height: 14px;
    font-family: roboto, helvetica;
    font-weight: 200;
    letter-spacing: 1px;
    text-decoration: none;
    box-shadow: 0 5px 3px rgba(0, 0, 0, 0.3);
    cursor: pointer;
  /*border-radius: 50px;*/
    -webkit-tap-highlight-color: transparent;
}

*[data-animation="ripple"]:focus {
    outline: none;
}

*[data-animation="ripple"]::selection {
    background: transparent;
    pointer-events: none;
}

JS:

const isMobile = window.navigator.userAgent.match(/Mobile/) && window.navigator.userAgent.match(/Mobile/)[0] === "Mobile";
const event = isMobile ? "touchstart" : "click";

const button = document.querySelectorAll('*[data-animation="ripple"]'),
            container = document.querySelector(".container");

for (var i = 0; i < button.length; i++) {
    const currentBtn = button[i];

    currentBtn.addEventListener(event, function(e) {

        e.preventDefault();
        const button = e.target,
                    rect = button.getBoundingClientRect(),
                    originalBtn = this,
                    btnHeight = rect.height,
                    btnWidth = rect.width;
        let posMouseX = 0,
                posMouseY = 0;

        if (isMobile) {
            posMouseX = e.changedTouches[0].pageX - rect.left;
            posMouseY = e.changedTouches[0].pageY - rect.top;
        } else {
            posMouseX = e.x - rect.left;
            posMouseY = e.y - rect.top;
        }

        const baseCSS =  `position: absolute;
                                            width: ${btnWidth * 2}px;
                                            height: ${btnWidth * 2}px;
                                            transition: all linear 700ms;
                                            transition-timing-function:cubic-bezier(0.250, 0.460, 0.450, 0.940);
                                            border-radius: 50%;
                                            background: var(--color-ripple);
                                            top:${posMouseY - btnWidth}px;
                                            left:${posMouseX - btnWidth}px;
                                            pointer-events: none;
                                            transform:scale(0)`

        var rippleEffect = document.createElement("span");
        rippleEffect.style.cssText = baseCSS;

        //prepare the dom
        currentBtn.style.overflow = "hidden";
        this.appendChild(rippleEffect);

        //start animation
        setTimeout( function() { 
            rippleEffect.style.cssText = baseCSS + `transform:scale(1); opacity: 0;`;
        }, 5);

        setTimeout( function() {
            rippleEffect.remove();
            //window.location.href = currentBtn.href;
        },700);
    })
}

是否有可能实现类似的功能而无需点击按钮而只是将鼠标悬停在按钮上?

2 个答案:

答案 0 :(得分:4)

替换JS的第2行:

const event = isMobile ? "touchstart" : "mouseover";

<强>与

#include <conio.h>
#include <graphics.h>
#include <dos.h>
#include <stdlib.h>

int main() {
    int gd = DETECT, gm;
    int i, x, y;
    initgraph(&gd, &gm, "C:\\TC\\BGI");
    initwindow(1200, 768);

      /* color 500 random pixels on screen */
   for(i=0; i<=125; i++) {
       x=rand()%getmaxx();
          y=rand()%getmaxy();
          putpixel(x,y,1);
      }

      for(i=0; i<=125; i++) {
       x=rand()%getmaxx();
          y=rand()%getmaxy();
          putpixel(x,y,2);
      }

      for(i=0; i<=125; i++) {
       x=rand()%getmaxx();
          y=rand()%getmaxy();
          putpixel(x,y,3);
      }

      for(i=0; i<=125; i++) {
       x=rand()%getmaxx();
          y=rand()%getmaxy();
          putpixel(x,y,4);
      }

      for(i=0; i<=125; i++) {
       x=rand()%getmaxx();
          y=rand()%getmaxy();
          putpixel(x,y,5);
      }

      for(i=0; i<=125; i++) {
       x=rand()%getmaxx();
          y=rand()%getmaxy();
          putpixel(x,y,6);
      }

      for(i=0; i<=125; i++) {
       x=rand()%getmaxx();
          y=rand()%getmaxy();
          putpixel(x,y,7);
      }

      for(i=0; i<=125; i++) {
       x=rand()%getmaxx();
          y=rand()%getmaxy();
          putpixel(x,y,8);
      }

      for(i=0; i<=125; i++) {
       x=rand()%getmaxx();
          y=rand()%getmaxy();
          putpixel(x,y,9);
      }
      for(i=0; i<=125; i++) {
       x=rand()%getmaxx();
          y=rand()%getmaxy();
          putpixel(x,y,9);
      }
      for(i=0; i<=125; i++) {
       x=rand()%getmaxx();
          y=rand()%getmaxy();
          putpixel(x,y,10);
      }
      for(i=0; i<=125; i++) {
       x=rand()%getmaxx();
          y=rand()%getmaxy();
          putpixel(x,y,11);
      }
      for(i=0; i<=125; i++) {
       x=rand()%getmaxx();
          y=rand()%getmaxy();
          putpixel(x,y,12);
      }
      for(i=0; i<=125; i++) {
       x=rand()%getmaxx();
          y=rand()%getmaxy();
          putpixel(x,y,13);
      }
      for(i=0; i<=100; i++) {
       x=rand()%getmaxx();
          y=rand()%getmaxy();
          putpixel(x,y,14);
      }

      for(i=0; i<=900; i++) {
       x=rand()%getmaxx();
          y=rand()%getmaxy();
          putpixel(x,y,15);
      }

    getch();
    closegraph();
    return 0;
}

那就可以胜任。

希望有所帮助!

答案 1 :(得分:2)

如果你想要修复纹波(例如从中间)而不是从鼠标进入的地方,答案就更简单了,不需要javascript:只需叠加一个半透明的圆形伪元素并为比例设置动画在悬停。

&#13;
&#13;
body {
	background: #36353c;
}

.container {
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	height: 50px;
	width: 200px;
	margin: auto;
}
.ripple{
	position: relative; /*Position relative is required*/
	height: 100%;
	width: 100%;
	display: block;
	outline: none;
	padding: 20px;
	color: #fff;
	text-transform: uppercase;
	background: linear-gradient(135deg, #e570e7 0%,#79f1fc 100%);
	box-sizing: border-box;
	text-align: center;
	line-height: 14px;
	font-family: roboto, helvetica;
	font-weight: 200;
	letter-spacing: 1px;
	text-decoration: none;
	box-shadow: 0 5px 3px rgba(0, 0, 0, 0.3);
	cursor: pointer;
	overflow:hidden;
}

.ripple:hover:before{
	animation: ripple 1s ease;
}

.ripple:before{
	content:"";
	position:absolute; top:0; left:0;
	width:100%; height:100%;
  background-color:rgba(255, 255, 255, 0.7);
  border-radius:50%;
	transform:scale(0);
}

@keyframes ripple{
	from{transform:scale(0); opacity:1;}
	to{transform:scale(3);opacity:0;}
}
&#13;
<div class="container">
	<a class="ripple">Hover Me</a>
</div>
&#13;
&#13;
&#13;