How can I make jscolor colorpicker to work on a div?

让人想犯罪 __ 提交于 2020-01-17 08:39:08

问题


I am using jscolor colorpicker which can only be attached to button element or input element.I want to use it for div.I tried this way-

https://jsfiddle.net/anuranpal/Lead7c7q/43/

CSS

edit-color-container {
    border: 1px solid gainsboro;
    height: 70px;
    width: 70px;
    text-align: center;
}

.select-button {
    background: none!important;
    border: none;
    padding: 0!important;
    cursor: pointer;
    display: block;
    width: 100%;
    height: 100%;
    -moz-user-select: none;
    -webkit-user-select: none;
    /* this will work for QtWebKit in future */
    -webkit-user-drag: none;
}

.selected-color-container {
    -moz-border-radius: 50px/50px;
    -webkit-border-radius: 50px 50px;
    border-radius: 50px/50px;
    width: 35px;
    height: 35px;
    background: #DF068C;
    display: inline-block;
    vertical-align: top;
    margin: auto;
    margin-top: 5px;
    position: relative;
}

HTML

<div class="edit-color-container">
    <input id="selected-color-value" type="hidden" value="#DF068C" />
    <button id="editColor" class="select-button jscolor " data-jscolor="
  {width:150, height:150,valueElement:'selected-color-
  value',styleElement:'selectedColor',borderWidth:0,borderColor:'#FFF', 
   insetWidth:0, insetColor:'#FFF',shadow:false, 
   backgroundColor:'#e6e7e9',borderRadius:2, zIndex:'2000'}">
        <div class="selected-color-container" id="selectedColor"></div>
        <div class="uk-text-small uk-text-primary uk-margin-small-top" style="margin:auto">Edit</div>
    </button>
</div>

But here I have used button instead of div and it is creating some issues in chrome like if I click on the circle,nothing happen but If I click just outside the circle, the color picker toggles.

So, I want to use div instead of button and open the colorpicker when I click on the div. Please help. Thank you in Advance :-)


回答1:


Seems like the plugin doesn't support div, but using its api you can toggle colorpicker using code, if I got you correctly, here is my solution:

HTML

<div class="edit-color-container">
    <div id="styleSpan" style="background-image: none; background-color: rgb(186, 243, 255); color: rgb(0, 0, 0);"
    onclick="document.getElementById('color-picker').jscolor.show()"></div>
    <div id="btn" onclick="document.getElementById('color-picker').jscolor.show()">Edit</div>
    <input id="color-picker" class="jscolor {styleElement:'styleSpan',value:'DF068C'}" type="hidden">
</div>

CSS

.edit-color-container {
    border: 1px solid gainsboro;
    height: 70px;
    width: 70px;
    text-align: center;
}

#styleSpan {
    width: 35px;
    height: 35px;
    border-radius: 50%;
    margin: 5px auto;
    cursor: pointer;
}

#btn {
    cursor: pointer;
}

body > div:last-child {
    margin: 50px 0 0 20px;
}

jsfiddle

Notice that you can use onclick on edit-color-container instead.



来源:https://stackoverflow.com/questions/44069862/how-can-i-make-jscolor-colorpicker-to-work-on-a-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!