如何使用 JavaScript 偏移一个轮廓并在边框边缘之外对其进行绘制?


如要偏移轮廓,请使用 outlineOffset 属性。它允许你在边框边缘之外绘制轮廓。

你可以尝试运行以下代码,以使用 JavaScript 偏移轮廓并在边框边缘之外对其进行绘制。

示例

实时演示

<!DOCTYPE html>
<html>
   <head>
      <style>
         #box {
            width: 450px;
            background-color: orange;
            border: 3px solid red;
            margin-left: 20px;
         }
      </style>
   </head>
   <body>
      <p>Click below to add Outline Offset.</p>
      <div id="box">
         <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
         <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
         <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
      </div>
      <br>
      <button type="button" onclick="display()">Set Outline Offset</button>
      <br>
      <script>
         function display() {
            document.getElementById("box").style.outline = "thick solid";
            document.getElementById("box").style.outlineColor = "#5F5F5F";
            document.getElementById("box").style.outlineOffset = "7px";
         }
      </script>
   </body>
</html>

更新于:30-7-2019

170 次浏览

开启你的事业

通过完成课程获得认证

开始
广告