:hover sticks to element on drag and drop

前端 未结 2 762
轻奢々
轻奢々 2021-02-19 18:40

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

2条回答
  •  执笔经年
    2021-02-19 19:32

    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

提交回复
热议问题