Preventing images and text to be selected

后端 未结 4 1402
一个人的身影
一个人的身影 2021-02-02 16:39

I have an image being hacked in as a background image (as shown here). I\'ve noticed that if I drag my mouse over it though, it selected the image so that it can\'t be deselect

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-02 17:23

    Add this to your style sheet

    .selectDisable {
        -webkit-user-select: none;
        -khtml-user-select: none;
        -moz-user-select: none;
        -o-user-select: none;
        user-select: none;
    }
    
    .selectEnable { 
        -webkit-user-select: text;
        -khtml-user-select: text;
        -moz-user-select: text;
        -o-user-select: text;
        user-select: text;
    }
    

    Just add the class selectDisable to the element you want to prevent from being selected.

    The drag effect occurs on webkit(chrome, safari, opera). It does not happen on Firefox.

    Don't this apply to your whole document if you have textual content because then you won't be able to select text, which is not very user-friendly.

    You could also prevent dragging by adding another empty div on top of your image with the same selectDisable class.

    Here is a working example:
    http://jsfiddle.net/xugy6shd/3/

提交回复
热议问题