:hover sticks to element on drag and drop(:hover 拖放到元素上)
问题描述
I have simple ol-li construction and want to add drag'n'drop to it. Additionaly I want to highlight hover item and dragover item in different colors. But it is an unusual bug in WebKit.
- Capture last item.
- Drag it to the top.
- Drop it to the first item.
And last element capture the hover pseudoclass! Why? How can I prevent it?
This is an example:
http://jsfiddle.net/zFk2V/3/
var lis = document.querySelectorAll("li"),
ol = document.querySelector("ol"),
dragged = false,
dragover = false;
ol.addEventListener("drop", function(event) {
ol.insertBefore(dragged,dragover);
this.classList.remove("insistent");
}, false);
for (var i=0, n = lis.length; i < n; i++) {
lis[i].addEventListener("dragstart", function(event) {
dragged = this;
ol.classList.add("insistent");
}, false);
lis[i].addEventListener("dragover", function(event) {
if (dragover) {
dragover.classList.remove("dragover");
}
event.preventDefault();
dragover = this;
this.classList.add("dragover");
}, false);
}
You can simply add an .onmouseover
and an .onmouseout
function and add/remove a class called hovered
instead of using CSS's :hover
. Here is the updated jsFiddle
Javascript to add (inside for loop)
lis[i].onmouseover = function() {
// Adds the 'hovered' class
this.className = this.className + " hovered";
}
lis[i].onmouseout = function() {
// Removes the 'hovered' class
this.className = this.className.split(' ').filter(function(v) {
return v!='hovered'
}).join(' ');
}
CSS
.hovered {
background: #fc9;
}
Side note: I might add an id to the ol
like id='dragableList'
and change the line var lis = document.querySelectorAll("li")
to var lis = document.getElementById('dragableList').querySelectorAll("li")
just in case you have another list somewhere in your project later on. Here is the jsFiddle with that included
这篇关于:hover 拖放到元素上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为::hover 拖放到元素上
基础教程推荐
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 我什么时候应该在导入时使用方括号 2022-01-01
- 动态更新多个选择框 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01