css3实现箭头效果的两种方式

具体原理不再赘述,网上有很多介绍,这里只做代码记录。
可以参考CSS三角形的实现原理及运用、用css制作空心箭头(上下左右各个方向均有),
一、盒模型
这种方式通过盒模型设置元素边框,关键在于移动的那1px(两个三角形错位1px形成箭头);


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>css制作空心的上下左右的箭头title>
    <style type="text/css">
        *{
            padding:0;
            margin:0;
        }
        .box{
            width:100px;
            height:500px;
            margin:0 auto;
            border:1px solid red;
            background:white;
        }
        .arrow-box{
            width:30px;
            height:30px;
            margin:20px auto;
            position:relative;
        }
        /*右箭头*/
        .right{
            width:20px;
            height:20px;
            position:absolute;
            left:0;
            top:0;
            border:1px solid blue;
        }
        .right-arrow1,.right-arrow2{
            width:0;
            height:0;
            display:block;
            position:absolute;
            left:0;
            top:0;
            border-top:10px transparent dashed;
            border-right:10px transparent dashed;
            border-bottom:10px transparent dashed;
            border-left:10px white solid;
            overflow:hidden;
        }
        .right-arrow1{
            left:1px;/*重要*/
            border-left:10px blue solid;
        }
        .right-arrow2{
            border-left:10px white solid;
        }
        /*左箭头*/
        .left{
            width:20px;
            height:20px;
            position:absolute;
            left:0;
            top:0;
            z-index: 2;/*兼容ie8-*/
            border:1px solid blue;
        }
        .left-arrow1,.left-arrow2{
            width:0;
            height:0;
            display:block;
            position:absolute;
            left:0;
            top:0;
            z-index:5;/*兼容ie8-*/
            border-top:10px transparent dashed;
            border-left:10px transparent dashed;
            border-bottom:10px transparent dashed;
            border-right:10px white solid;
            overflow:hidden;
        }
        .left-arrow1{
            border-right:10px blue solid;
        }
        .left-arrow2{
            left:1px;/*重要*/
            border-right:10px white solid;
        }
        /*上箭头*/
        .top{
            width:20px;
            height:20px;
            position:absolute;
            left:0;
            top:0;
            z-index: 2;/*兼容ie8-*/
            border:1px solid blue;
        }
        .top-arrow1,.top-arrow2{
            width:0;
            height:0;
            display:block;
            position:absolute;
            left:0;
            top:0;
            z-index: 5;/*兼容ie8-*/
            border-top:10px transparent dashed;
            border-left:10px transparent dashed;
            border-right:10px transparent dashed;
            border-bottom:10px white solid;
            overflow:hidden;
        }
        .top-arrow1{
            border-bottom:10px blue solid;
        }
        .top-arrow2{
            top:1px;/*重要*/
            border-bottom:10px white solid;
        }
        /*下箭头*/
        .bottom{
            width:20px;
            height:20px;
            position:absolute;
            left:0;
            top:0;
            z-index: 2;/*兼容ie8-*/
            border:1px solid blue;
        }
        .bottom-arrow1,.bottom-arrow2{
            width:0;
            height:0;
            display:block;
            position:absolute;
            left:0;
            top:0;
            z-index: 5;/*兼容ie8-*/
            border-bottom:10px transparent dashed;
            border-left:10px transparent dashed;
            border-right:10px transparent dashed;
            border-top:10px white solid;
            overflow:hidden;
        }
        .bottom-arrow1{
            top:1px;/*重要*/
            border-top:10px blue solid;
        }
        .bottom-arrow2{
            border-top:10px white solid;
        }
    style>

<body>
<div class="box">
    <p> 右箭头p>
    <div class="arrow-right arrow-box">
        <b class="right"><i class="right-arrow1">i><i class="right-arrow2">i>b>
    div>
    <p> 左箭头p>
    <div class="arrow-left arrow-box" >
        <b class="left"><i class="left-arrow1">i><i class="left-arrow2">i>b>
    div>
    <p> 上箭头p>
    <div class="arrow-top arrow-box" >
        <b class="top"><i class="top-arrow1">i><i class="top-arrow2">i>b>
    div>
    <p> 下箭头p>
    <div class="arrow-bottom arrow-box" >
        <b class="bottom"><i class="bottom-arrow1">i><i class="bottom-arrow2">i>b>
    div>
div>
body>
html>

二、css3 transform
这种方式通过旋转来实现,相对第一种代码较为简洁

<div data-v-3c5a6987="" class="item-box bod">
	<span data-v-3c5a6987="" class="title">负责人span> 
	<span data-v-3c5a6987="" class="content">span>
div>
.item-box span.content[data-v-3c5a6987] {
    position: relative;
    padding-right: 12px;
}
.item-box span.content[data-v-3c5a6987]::after {
    border: solid 2px #c8c8cd;
    border-bottom-width: 0;
    border-left-width: 0;
    content: " ";
    top: 50%;
    right: 0px;
    position: absolute;
    width: 5px;
    height: 5px;
    -webkit-transform: translateY(-50%) rotate(45deg);
    transform: translateY(-50%) rotate(45deg);
}

参考CSS三角形的实现原理及运用、用css制作空心箭头(上下左右各个方向均有)

你可能感兴趣的:(css)