• 技术文章 >web前端 >css教程

    CSS+JS实现爱心点赞按钮(代码示例)

    青灯夜游青灯夜游2021-10-29 10:13:38转载95
    本篇文章给大家介绍一下CSS+JS实现一个“爱之满满”点赞按钮的方法,希望对大家有所帮助!

    前段时间在看一档说唱节目,被里面的一个说唱歌手JBcob爱之满满这句词给洗脑了。

    于是这次给大家带来一个爱之满满的点赞按钮,让大家在点赞的同时还能感受到被爱包裹的感觉。

    1.png

    ToDoList

    Just Do It

    ❤️ 爱心按钮

    <!-- fullLove.html -->
    <div class="likeBtn" id="likeBtn">
        <span class="heart" id="heart"></span>
    </div>
    /* fullLove.css */
    .heart{
        background-color: #8a93a0;
        height: 13px;
        width: 13px;
        transform: rotate(-45deg) scale(1);
        display: inline-block;
    }
    .heart::before {
        content: '';
        position: absolute;
        top: -50%;
        left: 0;
        background-color: inherit;
        border-radius: 50%;
        height: 13px;
        width: 13px;
    }
    .heart::after {
        content: '';
        position: absolute;
        top: 0;
        right: -50%;
        background-color: inherit;
        border-radius: 50%;
        height: 13px;
        width: 13px;
    }

    2.png

    引导点赞

    // love.js
    const likeBtn = document.getElementById('likeBtn');
    const heart=document.getElementById('heart')
    likeBtn.addEventListener('mousemove',() => {
      heart.classList.add('heratPop')
    })
    likeBtn.addEventListener('mouseout',() => {
      heart.classList.remove('heratPop')
    })
    /* fullLove.css */
    .heratPop{
        animation: pulse 1s linear infinite;
    }
    @keyframes pulse {
        0% {
                transform: rotate(-45deg) scale(1);
        }
        10% {
                transform: rotate(-45deg) scale(1.1);
        }
        20% {
                transform: rotate(-45deg) scale(0.9);
        }
        30% {
                transform: rotate(-45deg) scale(1.2);
        }
        40% {
                transform: rotate(-45deg) scale(0.9);
        }
        50% {
                transform: rotate(-45deg) scale(1.1);
        }
        60% {
                transform: rotate(-45deg) scale(0.9);
        }
        70% {
                transform: rotate(-45deg) scale(1);
        }
    }

    3.gif

    爱之满满

    // love.js
    function addHearts(content) {
      for(let i=0; i<10; i++) {
        setTimeout(() => {
          const fullHeart = document.createElement('div');
          fullHeart.classList.add('hearts');
          fullHeart.innerHTML = '<span class="heart"></span>';
          fullHeart.style.left = Math.random() * 100 + '%';
          fullHeart.style.top = Math.random() * 100 + '%';
          fullHeart.style.transform = `translate(-50%, -50%) scale(${Math.random()+0.3}) `
          fullHeart.style.animationDuration = Math.random() * 2 + 3 + 's';
          fullHeart.firstChild.style.backgroundColor='#ed3056'
          content.appendChild(fullHeart);
          setTimeout(() => {
            fullHeart.remove();
          }, 3000);
        }, i * 100)
      }
    }
    /* fullLove.css */
    .hearts {
        position: absolute;
        color: #E7273F;
        font-size: 15px;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        animation: fly 3s linear forwards;
    }
    @keyframes fly {
        to {
            transform: translate(-50%, -50px) scale(0);
        }
    }

    4.gif

    写在最后

    更多编程相关知识,请访问:编程学习!!

    以上就是CSS+JS实现爱心点赞按钮(代码示例)的详细内容,更多请关注php中文网其它相关文章!

    声明:本文转载于:掘金社区,如有侵犯,请联系admin@php.cn删除
    上一篇:聊聊css为什么需要模块化?怎么进行模块化? 下一篇:normalize和css reset分别是什么文件?又有什么区别?
    大前端线上培训班

    相关文章推荐

    • 利用CSS怎么创建渐变色边框?5种方法分享• 8种CSS实现loading加载特效的小技巧(分享)• 10+个让你的项目大放异彩的CSS loading加载特效,快来收藏吧!!• postcss是什么东西?为什么要用?• 聊聊css为什么需要模块化?怎么进行模块化?

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网