HTML/CSS 可拖动元素

huangapple go评论65阅读模式
英文:

HTML/CSS Draggable elements

问题

I understand that you want a translation of the provided text, excluding the code. Here is the translation:

"你好。
我尝试构建一个使用相当简单的基于HTML/CSS的可拖动树状表示的应用程序。

问题是,当我尝试拖动一个分支(带有子项的项目)时,“ghost” 图像(可拖动元素的表示)具有非透明背景,无论我尝试什么都无法去掉。

我真的需要它不可见(ghost 图像仅显示当前拖动的分支的 leaf-handle)。

对于如何实现这一点的任何帮助或指导都将不胜感激。"

Please note that I've excluded the code portion as requested. If you need further assistance or have any questions, feel free to ask.

英文:

Good day.
I am trying to build an application with pretty simple HTML/CSS-based draggable tree representation.

<div class="tree">
  <div class="leaf" draggable=true>
    <div class="leaf-handle">item 1</div>
    <div class="leaf subleaf" draggable=true>
      <div class="leaf-handle">item 1.1</div>
      <div class="leaf subsubleaf" draggable=true>
        <div class="leaf-handle">item 1.1.1</div>
      </div>
    </div>
  </div>
  <div class="leaf" draggable=true>
    <div class="leaf-handle">item 2</div>
    <div class="leaf subleaf" draggable=true>
      <div class="leaf-handle">item 2.1</div>
      <div class="leaf subsubleaf" draggable=true>
        <div class="leaf-handle">item 2.1.1</div>
      </div>
    </div>
  </div>
</div>

Code example reproducing the case can be found here:
https://codepen.io/DerZinger/pen/bGQbEmd

Issue I'm facing is that when I try to drag a branch (item with subitems) the "ghost" image (representation of draggable elements) have non-transparent background, which I cannot remove regardless of what I try.

Like this:

HTML/CSS 可拖动元素

And I really need it to be non-visible (ghost image showing only leaf-handles of the branch, that is currently dragged).

Any help or guidance on HOW that can be achieved would be very appreciated.

Edit 1: Someone suggested removing background color. Tried that, and unfortunately that does not work, and that's the whole point.
Removing it, setting it opaque, and whatever - all these tricks do not ultimately "hide" that area which I want to hide:
HTML/CSS 可拖动元素

答案1

得分: 1

在测试了几种解决方案并进行了在线研究后,我最终提出了一个还算不错的解决方案;虽然不是完美的,但是是目前可用的最佳解决方案。

以下是代码部分,无需翻译:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

document.addEventListener("dragstart", function (event) {
  document.querySelector(".tree").style.backgroundColor = "transparent";
  event.target.style.background = "transparent";
  setTimeout(() => {
    document.querySelector(".tree").style.backgroundColor = "red";
    event.target.style.background = "yellow";
  }, 100);
});

<!-- language: lang-css -->

.leaf {
  background-color: yellow;
  width: 200px;
}
.leaf-handle {
  background-color: green;
  border: 1px solid black;
}
.tree {
  background-color: red;
  display: flex;
  flex-direction: column;
}

.subleaf {
  padding-left: 30px;
}
.subsubleaf {
  margin-left: 60px;
}

<!-- language: lang-html -->

<div class="tree">
  <div class="leaf" draggable="true">
    <div class="leaf-handle">item 1</div>
    <div class="subleaf">
      <div class="leaf-handle">item 1.1</div>
      <div class="subsubleaf">
        <div class="leaf-handle">item 1.1.1</div>
      </div>
    </div>
  </div>
  <div class="leaf" draggable="true">
    <div class="leaf-handle">item 2</div>
    <div class="leaf subleaf" draggable="true">
      <div class="leaf-handle">item 2.1</div>
      <div class="leaf subsubleaf" draggable="true">
        <div class="leaf-handle">item 2.1.1</div>
      </div>
    </div>
  </div>
</div>
<div class="tree">
  <div class="leaf" draggable="true">
    <div class="leaf-handle">item 1</div>
    <div class="leaf subleaf" draggable="true">
      <div class="leaf-handle">item 1.1</div>
      <div class="leaf subsubleaf" draggable="true">
        <div class="leaf-handle">item 1.1.1</div>
      </div>
    </div>
  </div>
  <div class="leaf" draggable="true">
    <div class="leaf-handle">item 2</div>
    <div class="leaf subleaf" draggable="true">
      <div class="leaf-handle">item 2.1</div>
      <div class="leaf subsubleaf" draggable="true">
        <div class="leaf-handle">item 2.1.1</div>
      </div>
    </div>
  </div>
</div>

<!-- end snippet -->
英文:

After testing several workarounds with your problem and doing online research about it, I came up finally with a decent solution; not the perfect one but the best available today.

Code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

document.addEventListener(&quot;dragstart&quot;, function (event) {
document.querySelector(&quot;.tree&quot;).style.backgroundColor = &quot;transparent&quot;;
event.target.style.background = &quot;transparent&quot;;
setTimeout(() =&gt; {
document.querySelector(&quot;.tree&quot;).style.backgroundColor = &quot;red&quot;;
event.target.style.background = &quot;yellow&quot;;
}, 100);
});

<!-- language: lang-css -->

.leaf {
background-color: yellow;
width: 200px;
}
.leaf-handle {
background-color: green;
border: 1px solid black;
}
.tree {
background-color: red;
display: flex;
flex-direction: column;
}
.subleaf {
padding-left: 30px;
}
.subsubleaf {
margin-left: 60px;
}

<!-- language: lang-html -->

&lt;div class=&quot;tree&quot;&gt;
&lt;div class=&quot;leaf&quot; draggable=&quot;true&quot;&gt;
&lt;div class=&quot;leaf-handle&quot;&gt;item 1&lt;/div&gt;
&lt;div class=&quot;subleaf&quot;&gt;
&lt;div class=&quot;leaf-handle&quot;&gt;item 1.1&lt;/div&gt;
&lt;div class=&quot;subsubleaf&quot;&gt;
&lt;div class=&quot;leaf-handle&quot;&gt;item 1.1.1&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;leaf&quot; draggable=&quot;true&quot;&gt;
&lt;div class=&quot;leaf-handle&quot;&gt;item 2&lt;/div&gt;
&lt;div class=&quot;leaf subleaf&quot; draggable=&quot;true&quot;&gt;
&lt;div class=&quot;leaf-handle&quot;&gt;item 2.1&lt;/div&gt;
&lt;div class=&quot;leaf subsubleaf&quot; draggable=&quot;true&quot;&gt;
&lt;div class=&quot;leaf-handle&quot;&gt;item 2.1.1&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;tree&quot;&gt;
&lt;div class=&quot;leaf&quot; draggable=&quot;true&quot;&gt;
&lt;div class=&quot;leaf-handle&quot;&gt;item 1&lt;/div&gt;
&lt;div class=&quot;leaf subleaf&quot; draggable=&quot;true&quot;&gt;
&lt;div class=&quot;leaf-handle&quot;&gt;item 1.1&lt;/div&gt;
&lt;div class=&quot;leaf subsubleaf&quot; draggable=&quot;true&quot;&gt;
&lt;div class=&quot;leaf-handle&quot;&gt;item 1.1.1&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;leaf&quot; draggable=&quot;true&quot;&gt;
&lt;div class=&quot;leaf-handle&quot;&gt;item 2&lt;/div&gt;
&lt;div class=&quot;leaf subleaf&quot; draggable=&quot;true&quot;&gt;
&lt;div class=&quot;leaf-handle&quot;&gt;item 2.1&lt;/div&gt;
&lt;div class=&quot;leaf subsubleaf&quot; draggable=&quot;true&quot;&gt;
&lt;div class=&quot;leaf-handle&quot;&gt;item 2.1.1&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月4日 23:23:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定