js+css实现液态按钮悬浮交互动画效果代码
代码语言:html
所属分类:悬停
代码描述:js+css实现液态按钮悬浮交互动画效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Droid+Sans:700'> <style> body { display: flex; height: 100vh; align-items: center; justify-content: center; } .btn-liquid { display: inline-block; position: relative; width: 240px; height: 60px; border-radius: 27px; color: #fff; font: 700 14px/60px "Droid Sans", sans-serif; letter-spacing: 0.05em; text-align: center; text-decoration: none; text-transform: uppercase; } .btn-liquid .inner { position: relative; z-index: 2; } .btn-liquid canvas { position: absolute; top: -50px; right: -50px; bottom: -50px; left: -50px; z-index: 1; } </style> </head> <body> <!-- partial:index.partial.html --> <a href="" class="btn-liquid"> <span class="inner">Liquid button ?</span> </a> <!-- partial --> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/jquery-3.2.1.min.js"></script><script > $(function() { // Vars var pointsA = [], pointsB = [], $canvas = null, canvas = null, context = null, vars = null, points = 8, viscosity = 20, mouseDist = 70, damping = 0.05, showIndicators = false; mouseX = 0, mouseY = 0, relMouseX = 0, relMouseY = 0, mouseLastX = 0, mouseLastY = 0, mouseDirectionX = 0, mouseDirectionY = 0, mouseSpeedX = 0, mouseSpeedY = 0; /** * Get mouse direction */ function mouseDirection(e) { if (mouseX < e.pageX) mouseDirectionX = 1; else if (mouseX > e.pageX) mouseDirectionX = -1; else mouseDirectionX = 0; if (mouseY < e.pageY) mouseDirectionY = 1; else if (mouseY > e.pageY) mouseDirectionY = -1; else mouseDirectionY = 0; mouseX = e.pageX; mouseY = e.pageY; relMouseX = (mouseX - $canvas.offset().left); relMouseY = (mouseY - $canvas.offset().top); } $(document).on('mousemove', mouseDirection); /** * Get mouse speed */ function mouseSpeed() { mouseSpeedX = mouseX - mouseLastX; mouseSpeedY = mouseY - mouseLastY; mouseLastX = mouseX; mouseLastY = mouseY; setTimeout(mouseSpeed, 40); } mouseSpeed(); /** * Init button */ function initButton() { // Get button var button = $('.btn-liquid'); var buttonWidth = button.width(); var buttonHeight = button.height(); // Create canvas $canvas = $('<canvas></canvas>'); button.append($canvas); canvas = $canvas.get(0); canvas.width = buttonWidth+100; canvas.height = buttonHeight+100; context = canvas.getContext('2d'); // Add points var x = buttonHeight/2; for(var j = 1; j < points; j++) { addPoints((x+((buttonWidth-buttonHeight)/points)*j), 0); } addPoints(buttonWidth-buttonHeight/5, 0); addPoints(buttonWidth+buttonHeight/10, buttonHeight/2); addPoints(buttonWidth-buttonHeight/5, buttonHeight); for(var j = points-1; j > 0; j--) { addPoints((x+((buttonWidth-buttonHeight)/points)*j), buttonHeight); } addPoints(buttonHeight/5, buttonHeight); addPoints(-buttonHeight/10, buttonHeight/2); addPoints(buttonHeight/5, 0); // addPoints(x, 0); // addPoints(0, buttonHeight/2); // addPoints(0, buttonHeight/2); // addPoints(buttonHeight/4, 0); // Start render renderCanvas(); } /** * Add points */ function addPoints(x, y) { pointsA.push(new Point(x, y, 1)); pointsB.push(new Point(x, y, 2)); } /** * Point */ function Point(x, y, level) { this.x = this.ix = 50+x; this.y = this.iy = 50+y; this.vx = 0; this.vy = 0; this.cx1 = 0; this.cy1 = 0; this.cx2 = 0; this.cy2 = 0; this.level = level; } Point.prototype.move = function() { this.vx += (this.ix - this.x) / (viscosity*this.level); this.vy += (this.iy - this.y) / (viscosity*this.level); var dx = this.ix - relMouseX, dy = this.iy.........完整代码请登录后点击上方下载按钮下载查看
网友评论0