HTML实现弹出层

leopard/ˈlepərd/ 豹子,豹纹

弹出层指的是鼠标悬停于某个元素之上时显示的一个界面组件。

关注和理解特性:z-index属性和动态生成HTML元素。

HTML5新增:

figure:媒体内容(图像,音频,视频),用于包含一组相关元素。

figcaption:为figure提供说明性文本,在figure中只能 有一个,并且在第一个或者最后一个位置。

并排显示:某度上的图片

代码:6.3.弹出层.html

html 复制代码
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <style>
        figure {
            width: 144px;
            height: 153px;
            margin: 20px 20px;
            border: 1px solid #666;
            position: relative;
            float: left;
        }

        img {
            width: 144px;
            height: 153px;
            display: block;
        }
    </style>
</head>

<body>
    <figure>
        <img src="img/pink.jpg" alt="pink heels" />
        <figcaption>
            <h3>Pink Platforms</h3>
            <a href="#">More info</a>
        </figcaption>
    </figure>
    <figure class="click_me">
        <img src="img/baowen.jpg" alt="baowen heels" />
        <figcaption>
            <h3>Leopard Platforms</h3>
            <a href="#">More info</a>
        </figcaption>
    </figure>
    <figure class="click_me">
        <img src="img/red.jpg" alt="red heels" />
        <figcaption>
            <h3>Red Platforms</h3>
            <a href="#">More info</a>
        </figcaption>
    </figure>
</body>

</html>

6.3.1 堆叠上下文z-index

隐藏弹出层,悬停显示弹出层

增加CSS:

html 复制代码
figcaption {
            display: none;
            /* 隐藏弹出层 */
            position: absolute;
            /* 相对于容器 */
            left: 74%;
            /* 放到右边 */
            top: 15px;
            width: 130px;
            /* 弹出层宽度 */
            padding: 10px;
            /* 弹出层内边距 */
            background-color: #f2eaea;
            border: 3px solid red;
            border-radius: 6px;
        }

        /* 鼠标悬停在图片上显示 */
        figure:hover figcaption {
            display: block;
        }

        figcaption h3 {
            /*弹出层的内容*/
            font-size: 14px;
            color: #666;
            margin-bottom: 6px;
        }

        figcaption a {
            /*弹出层的内容*/
            display: block;
            text-decoration: none;
            font-size: 12px;
            color: #000;
        }
        
    /* 把弹出层设置为最高层 */
    figure:hover figcaption {
        display: block;
        z-index: 2;
    }

6.3.2 用CSS创造三角形

用div来创建,角上是平分的,所以要是内部宽高为0,其他边透明,正好是三角形。

代码:

html 复制代码
div {
    border: 12px solid;
    width: 0;
    height: 0;
    border-color: transparent red transparent transparent;
}

与伪元素after结合起来,在6.3.弹出层.html中增加CSS:

html 复制代码
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <style>
        figure {
            width: 144px;
            height: 153px;
            margin: 20px 20px;
            border: 1px solid #666;
            position: relative;
            float: left;
        }

        img {
            width: 144px;
            height: 153px;
            display: block;
        }

        figcaption {
            display: none;
            /* 隐藏弹出层 */
            position: absolute;
            /* 相对于容器 */
            left: 74%;
            /* 放到右边 */
            top: 15px;
            width: 130px;
            /* 弹出层宽度 */
            padding: 10px;
            /* 弹出层内边距 */
            background-color: #f2eaea;
            border: 3px solid red;
            border-radius: 6px;
        }

        /* 鼠标悬停在图片上显示 */
        figure:hover figcaption {
            display: block;
        }

        figcaption h3 {
            /*弹出层的内容*/
            font-size: 14px;
            color: #666;
            margin-bottom: 6px;
        }

        figcaption a {
            /*弹出层的内容*/
            display: block;
            text-decoration: none;
            font-size: 12px;
            color: #000;
        }

        /* 把弹出层设置为最高层 */
        figure:hover figcaption {
            display: block;
            z-index: 2;
        }

        /* 红色三角形盒子 */
        figcaption::after {
            content: "";
            position: absolute;
            border: 12px solid;
            border-color: transparent red transparent transparent;
            height: 0px;
            width: 0px;
            top: 17px;
            right: 100%;
        }
    </style>
</head>

<body>
    <figure>
        <img src="img/pink.jpg" alt="pink heels" />
        <figcaption>
            <h3>Pink Platforms</h3>
            <a href="#">More info</a>
        </figcaption>
    </figure>
    <figure class="click_me">
        <img src="img/baowen.jpg" alt="baowen heels" />
        <figcaption>
            <h3>Leopard Platforms</h3>
            <a href="#">More info</a>
        </figcaption>
    </figure>
    <figure class="click_me">
        <img src="img/red.jpg" alt="red heels" />
        <figcaption>
            <h3>Red Platforms</h3>
            <a href="#">More info</a>
        </figcaption>
    </figure>
</body>

</html>
相关推荐
yanglamei1962几秒前
基于GIKT深度知识追踪模型的习题推荐系统源代码+数据库+使用说明,后端采用flask,前端采用vue
前端·数据库·flask
千穹凌帝1 分钟前
SpinalHDL之结构(二)
开发语言·前端·fpga开发
dot.Net安全矩阵12 分钟前
.NET内网实战:通过命令行解密Web.config
前端·学习·安全·web安全·矩阵·.net
Hellc00723 分钟前
MacOS升级ruby版本
前端·macos·ruby
前端西瓜哥32 分钟前
贝塞尔曲线算法:求贝塞尔曲线和直线的交点
前端·算法
又写了一天BUG33 分钟前
npm install安装缓慢及npm更换源
前端·npm·node.js
cc蒲公英1 小时前
Vue2+vue-office/excel 实现在线加载Excel文件预览
前端·vue.js·excel
Java开发追求者1 小时前
在CSS中换行word-break: break-word和 word-break: break-all区别
前端·css·word
好名字08211 小时前
monorepo基础搭建教程(从0到1 pnpm+monorepo+vue)
前端·javascript
pink大呲花1 小时前
css鼠标常用样式
前端·css·计算机外设