代码驿站移动版
频道导航
HTML/Xhtml
CSS
JavaScript
HTML5
PHP教程
ASP.NET
正则表达式
AJAX
ThinkPHP
Yii
MySQL
MariaDB
Oracle
MongoDB
Redis
DedeCMS
PHPCMS
帝国CMS
WordPress
Discuz
其它CMS
Zend Studio
Sublime
Notepad
Dreamweaver
Windows
Linux
Nginx
Apache
IIS
CentOS
Ubuntu
Debian
网站优化
工具资源
PHP源码
ASP.NET源码
其它源码
图标素材
按钮素材
字体素材
DedeCMS模板
帝国CMS模板
PHPCMS模板
WordPress模板
Discuz!模板
单页模板
开发软件下载
服务器软件下载
广告投放
联系我们
版权申明
软件编程
网页前端
移动开发
数据库
服务器
脚本语言
PHP代码
JAVA代码
Python代码
Android代码
当前位置:
主页
>
网页前端
>
JavaScript代码
>
使用button标签,实现三态图片按钮
时间:2021-04-27 09:09:42 | 栏目:
JavaScript代码
| 点击:次
三态图片按钮对于好的用户体验来说必不可少。我对dom事件进行一些处理,在不改变html的情况下,实现了三态按钮。下面的解决方法的关键,主要是去掉虚线选框和对背景进行定位,没有使用循环处理,任何一个按钮,只要定义了背景图片和边框为0,就自动变成了三态按钮。
<body> <button style="width:53;height:17;border:0;background:url(https://www.jb51.net/upload/button_a.png)"></button> </body> <script> if(document.all) document.execCommand("BackgroundImageCache",false,true) document.body.onload=function(){ if(document.activeElement.tagName=="BUTTON") document.activeElement.blur() } document.onmouseover=document.onmouseout=document.onmousedown=document.onmouseup=function(e){ var ee=e==null?event.srcElement:e.target e=e||event if(ee.tagName!="BUTTON"||ee.clientWidth!=ee.offsetWidth) return if(e.type=="mousedown"){ ee.style.backgroundPosition="0 -"+(2*ee.offsetHeight) if(document.all) ee.hideFocus=true else return false } if(e.type=="mouseover"||e.type=="mouseup") ee.style.backgroundPosition="0 -"+ee.offsetHeight if(e.type=="mouseout") ee.style.backgroundPosition="0 0" } </script>
[Ctrl+A 全选 注:
引入外部Js需再刷新一下页面才能执行
]
上面的解决方法还有点美中不足,就是在firefox下,没办法用Tab键定位按钮,这是因为为了去掉虚框,屏蔽了点击事件。下面的程序解决了这个问题,方法是在按钮里创建一个不可见的新按钮,由它转移焦点。
<style> button{border:0} </style> <body> <button style="width:53;height:17;background:url(https://www.jb51.net/upload/button_a.png)"></button> </body> <script> if(document.all) document.execCommand("BackgroundImageCache",false,true) document.body.onload=function(){ if(document.activeElement.tagName=="BUTTON") document.activeElement.blur() } document.onmouseover=document.onmouseout=document.onmousedown=document.onmouseup=function(e){ var o,ee=e==null?event.srcElement:e.target e=e||event if(ee.tagName=="BUTTON"&&ee.clientWidth==ee.offsetWidth){ if(e.type=="mousedown"){ if((ee.lastChild||"").tagName!="BUTTON"){ o=document.createElement("button") o.style.cssText="position:absolute;left:-9000" ee.appendChild(o) ee.onfocus=function(){ee.lastChild.focus()} } ee.style.backgroundPosition="0 -"+(2*ee.offsetHeight) } if(e.type=="mouseover"||e.type=="mouseup") ee.style.backgroundPosition="0 -"+ee.offsetHeight if(e.type=="mouseout") ee.style.backgroundPosition="0 0" } } </script>
[Ctrl+A 全选 注:
引入外部Js需再刷新一下页面才能执行
]
您可能感兴趣的文章:
JavaScript常见事件处理程序实例总结
Javascript的匿名函数小结
url传递的参数值中包含&时,url自动截断问题的解决方法
Javascript别踩白块儿(钢琴块儿)小游戏实现代码
兼容浏览器的js事件绑定函数(详解)
相关文章
10-19
javascript上下左右定时滚动插件
11-23
Webpack中loader打包各种文件的方法实例
11-27
Javascript实例教程(19) 使用HoTMetal(2)
10-05
javascript判断图片是否加载完成的方法推荐
11-22
在javascript中执行任意html代码的方法示例解读
JQuery
VUE
AngularJS
MSSql
MySQL
MongoDB
Redis
Linux
Tomcat
Nginx
网站首页
广告投放
联系我们
版权申明
联系站长