HTML+CSS制作鼠标悬停效果

html-css012

HTML+CSS制作鼠标悬停效果,第1张

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>鼠标悬停效果</title>

    <style type="text/css">

        *{

            margin: 0

            padding: 0

        }

        body{

            background-color: #000

        }

        a{

            width: 200px

            height: 50px

            display: block

            text-align: center

            line-height: 50px

            text-decoration: none

            position: absolute

            top: 50%

            left: 50%

            transform: translate(-50%,-50%)

            font-size: 24px

            font-weight: bold

            color: white

            border: 1px solid white

            overflow: hidden

        }

        a::before{

            content: ""

            position: absolute

            top: 0

            left: -100%

            width: 100%

            height: 100%

            background-image: linear-gradient(60deg,transparent,rgba(146,148,248,.4),transparent)

            transition: all 0.5s

        }

        a:hover::before{

            left: 100%

        }

        a:hover{

            box-shadow: 0 2px 10px 8px rgba(146,148,248,.4)

        }

    </style>

</head>

<body>

    <a href="#">鼠标悬停效果</a>

</body>

</html>

CSS+HTML<悬停下划线效果>

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>Document</title>

    <style>

        body {

            display: flex

            height: 100vh

            justify-content: center

            align-items: center

        }

        ul {

            padding: 0

            margin: 0

            list-style-type: none

        }

        ul li{

            padding: 5px 0

        }

        ul li a {

            position: relative

            display: inline-block

            text-decoration: none

            color: #3a3a3a

            /* 转大写 */

            text-transform: uppercase

            padding: 4px 0

            transition: 0.5s

            font-weight: bold

        }

        ul li a::after {

            position: absolute

            content: ""

            width: 100%

            height: 3px

            top: 100%

            left: 0

            background: #347cdb

            transition: transform 0.5s

            transform: scaleX(0)

            transform-origin: right

        }

        ul li a:hover {

            color: #585858

            text-shadow: 0 0 10px #ccc

        }

        ul li a:hover::after {

            transform: scaleX(1)

            transform-origin: left

        }

    </style>

</head>

<body>

    <ul>

        <li><a href="#">home</a></li>

        <li><a href="#">archives</a></li>

        <li><a href="#">tags</a></li>

        <li><a href="#">categories</a></li>

        <li><a href="#">about</a></li>

    </ul>

</body>

</html>

解析:前面的url是自定义鼠标图标的路径,可以为相对/绝对路径。第二个参数是css标准的cursor样式。可换成其他属性(如pointer/crosshair/default/等)

注意:w3school推荐第二个参数必须定义一个普通的光标,以防止url定义的光标有备用选项。另外,IE下第二个参数可以省略。

自定义鼠标图标,需要注意以下几点:

① 图标的格式

IE支持cur,ani,ico这三种格式,FF支持bmp,gif,jpg,cur,ico这几种格式,不支持ani格式,也不支持gif动画格式,因此一般将url引用的图标存为ico或cur格式比较好。

② 图标的大小

鼠标图标的尺寸推荐32*32,否则可能出现大小不一致问题。

参考文章: 使用自定义的鼠标图标