javascript - 从一个div的左下角画一条路径到某个目的地

标签 javascript html css responsive

抱歉标题模糊。我不确定如何表达这一点。 所以我想从 div 的左下角(或右下角)到图像中的某个点画一条线。实现这一目标的最佳响应方式是什么?我在这里画了一个例子。

enter image description here

所以我的猜测是像::after 这样的伪元素,但我如何获得这种 flex 以及如何保持响应?如果有人有一些站点示例,我也会很感激。

最佳答案

Canvas 可能有点矫枉过正,但您可以这样做:

CSS 很简单。您将需要一个容器元素、一个 Canvas 元素和图像描述元素。

#vegetable-image {
    /* container */
    position: relative;
}
#vegetable-image > canvas {
    /* This contains your image. The width and height is set in the canvas element */
    background:url(vegetables.jpg);
    background-size:cover;
}
#vegetable-image > div {
    /* style for the element from which you're drawing the line */
    position: absolute;
    top: 20px;
    left: 20px;
    width: 300px;
    height: 200px;
    background: white;
    padding: 10px;
}

HTML:

<div id="vegetable-image">
    <canvas width="600" height="300"></canvas>
    <div><h3>Eat eat eat</h3><p>lorem ipsum dolor sit amet.</p></div>
</div>

JavaScript 获取 Canvas 和描述框,计算框的右下角(减去 10px,这样您就看不到线的末端),然后绘制线。第一个 lineTo 命令 ctx.lineTo(380,260); 包含您要将线绘制到的坐标。紧随其后的第二个, ctx.lineTo(380,280);,绘制到低 20px 的点以提供 flex 。

var vegetable = document.getElementById("vegetable-image");
var canvas = vegetable.getElementsByTagName("canvas")[0];
var tag = vegetable.getElementsByTagName("div")[0];
var ctx = canvas.getContext("2d");
var lineStartLeft = tag.offsetLeft + tag.offsetWidth - 10; // 10 px back from right edge
var lineStartTop = tag.offsetTop + tag.offsetHeight - 10; // 10 px back from bottom

ctx.beginPath();
ctx.moveTo(lineStartLeft,lineStartTop);
ctx.lineTo(380,260);
ctx.lineTo(380,280);
ctx.lineWidth = 10;  // line thickness
ctx.strokeStyle = '#ffffff';  // line colour
ctx.stroke();

为了响应,您可以设置一个 css 媒体查询以将描述框位置设置为相对位置,并在移动设备上禁用 Canvas 线条绘制。

关于javascript - 从一个div的左下角画一条路径到某个目的地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51928581/

相关文章:

javascript - chrome devtools 调试 react ,命中 "refresh"导致调试卡住

javascript - 将选择框值附加到文本区域

html - Bootstrap data-affix/position-fixed col 与其他 col 重叠

html - Primefaces CSS覆盖单个组件

javascript - Td 左边框不适用

javascript - 通过 .catch() 传播被拒绝的 promise

javascript - 显示html页面中具有相同id的所有复选框元素

html - 如何设置平行DIV?

css - 转换后应用过滤器

jquery - 在版本 3.2.1 中使用 css 样式上传按钮