我需要制作一个在任何现代浏览器和任何操作系统上都相同的按钮(appearance: button
-这对我来说不是解决方案!)。
These are the 4 stages (without disabled stage) of the button I want to recreate.
我做了一个包装纸,但是有一个problem with the gradient。这可以通过复制原始按钮的图像并将其作为背景插入来解决,但是我认为这是一个错误的决定。我的观点是,与使用background-image: url('https://i.imgur.com/lmEjKKe.png')
的临时版本相比,渐变是一种更好的缩放解决方案。
//
function setBtnHover(element, value) {
//
element.dataset.hover = value;
}
//
var lastBtnMouseDown = null;
//
function setBtnMouseDown(element, value, e) {
//
if (element !== null) {
//
element.dataset.mouseDown = value;
//
if (value) {
//
if (lastBtnMouseDown !== null)
//
lastBtnMouseDown.dataset.click = false;
//
element.dataset.click = true;
//
lastBtnMouseDown = element;
}
//
e.stopPropagation();
//
} else if (!value) {
//
if (lastBtnMouseDown !== null) {
//
lastBtnMouseDown.dataset.mouseDown = value;
}
} else {
//
lastBtnMouseDown.dataset.click = false;
//
lastBtnMouseDown = null;
}
}
.button {
box-shadow: -1px -1px 0 0 rgba(0,0,0,0.05);
padding-left: 10px;
padding-right: 10px;
height: 22px;
box-sizing: border-box;
border: 1px solid #003c74;
display: inline-block;
color: #000000;
cursor: default;
text-align: center;
font: 13.3333px MS Shell Dlg \32;
padding-top: 2px;
border-radius: 3px;
background-image: url('https://i.imgur.com/3Jwqiw1.png');
-moz-user-select: none;
user-select: none;
}
.button[data-hover=false][data-click=true] {
background: radial-gradient(white 60%, blue 100%);
}
.button[data-hover=true][data-mouse-down=true] {
padding-left: 1px;
background-image: url('https://i.imgur.com/lmEjKKe.png');
}
.button[data-hover=true][data-mouse-down=false] {
background: radial-gradient(white 60%, orange 100%);
}
<body onmousedown="setBtnMouseDown(null, true)" onmouseup="setBtnMouseDown(null, false)">
<p style="padding-left: 1px;"><span class="button" data-hover="false" data-mouse-down="false" data-click="false" onmouseover="setBtnHover(this, true)" onmouseout="setBtnHover(this, false)" onmousedown="setBtnMouseDown(this, true, event)" onmouseup="setBtnMouseDown(this, false, event)">BUTTON</span></p>
<p><button>BUTTON</button></p>
</body>
JSFiddle:https://jsfiddle.net/0wd4m6qg/5/
答案 0 :(得分:0)
关闭屏幕截图,我想这就是您想要的效果。我正在堆叠伪元素以创建所需的效果。
//
function setBtnHover(element, value) {
//
element.dataset.hover = value;
}
//
var lastBtnMouseDown = null;
//
function setBtnMouseDown(element, value, e) {
//
if (element !== null) {
//
element.dataset.mouseDown = value;
//
if (value) {
//
if (lastBtnMouseDown !== null)
//
lastBtnMouseDown.dataset.click = false;
//
element.dataset.click = true;
//
lastBtnMouseDown = element;
}
//
e.stopPropagation();
//
} else if (!value) {
//
if (lastBtnMouseDown !== null) {
//
lastBtnMouseDown.dataset.mouseDown = value;
}
} else {
//
lastBtnMouseDown.dataset.click = false;
//
lastBtnMouseDown = null;
}
}
.button {
box-shadow: -1px -1px 0 0 rgba(0, 0, 0, 0.05);
width: 70px;
height: 23px;
box-sizing: border-box;
border: 1px solid #003c74;
display: inline-block;
color: #000000;
cursor: default;
text-align: center;
font: 13.3333px MS Shell Dlg \32;
padding-top: 2px;
border-radius: 3px;
-moz-user-select: none;
user-select: none;
position: relative;
z-index: 0;
}
.button::before {
position: absolute;
background-image: url('https://i.imgur.com/3Jwqiw1.png');
content: "";
left: 0;
right: 0;
top: 0;
bottom: 0;
z-index: -2;
}
.button::after {
position: absolute;
background: #fff;
content: "";
left: 2px;
right: 2px;
top: 2px;
bottom: 2px;
z-index: -1;
}
.button[data-hover=false][data-click=true]::before {
background: radial-gradient(white 60%, blue 100%);
}
.button[data-hover=true][data-mouse-down=true]::before {
padding-left: 1px;
background-image: url('https://i.imgur.com/lmEjKKe.png');
}
.button[data-hover=true][data-mouse-down=false]::before {
background: radial-gradient(white 60%, orange 100%);
}
<body onmousedown="setBtnMouseDown(null, true)" onmouseup="setBtnMouseDown(null, false)">
<p style="padding-left: 1px;"><span class="button" data-hover="false" data-mouse-down="false" data-click="false" onmouseover="setBtnHover(this, true)" onmouseout="setBtnHover(this, false)" onmousedown="setBtnMouseDown(this, true, event)" onmouseup="setBtnMouseDown(this, false, event)">BUTTON</span></p>
<p><button>BUTTON</button></p>
</body>
答案 1 :(得分:0)
您可以使用类来应用CSS来代替伪类或伪元素(我认为这是更简单,更简洁的代码),并且可以使用“插入”中的盒形阴影代替径向渐变背景。模式以创建粗大的内部边框(加上纯色背景色或线性渐变):
.my-button.over {
/* background: radial-gradient(white 60%, orange 100%); */
box-shadow: 0 0 0 0.25em orange inset;
}
您还可以通过将事件侦听器添加到要定位的选择器来简化逻辑,如下所示:
var mybutton = document.querySelector('.my-button'); // single element
var buttons = document.querySelectorAll('.my-button'); // many elements
然后创建一些向目标元素添加类或从中删除类的函数:
function overButton() {
this.classList.add('over')
}
使用正确的事件侦听器将功能附加到元素。 (您还可以添加条件逻辑来检查是否已将类添加到特定按钮。)
mybutton.addEventListener('mouseout', outButton); // single element
// see my JS Bin for attaching to multiple elements.
这是我的JS Bin: