[英]How do you make an image change on click and revert upon release using HTML/CSS/JS
I have an issue with Styling an object to make it look like a button and act as a functional link. 我对样式对象有一个问题,使它看起来像一个按钮并充当功能链接。
The effect i'm trying to achieve is to represent a button, so when clicked, it will begin to load the target page, but animate the object to visually represent a button being pressed. 我试图实现的效果是表示一个按钮,因此当单击该按钮时,它将开始加载目标页面,但会为该对象赋予动画以可视地表示被按下的按钮。
Here is a (non-working) JS Fiddle as an example of the desired effect. 这是一个(无效的) JS Fiddle作为所需效果的示例。
The first fiddle has no JavaScript in it for now, as i am currently a novice when it comes to manually typing it. 第一个小提琴目前暂时没有JavaScript,因为在手动输入它时,我目前还是一个新手。
I have tried using pseudo classes, such as :hover
as a temporary solution, but it doesn't work as desired, and from what i've tried, every possible solution i have seen on other questions don't seem to resolve my problem 我尝试使用伪类,例如
:hover
作为临时解决方案,但是它不能按需工作,从我的尝试来看,我在其他问题上看到的所有可能的解决方案似乎都无法解决我的问题
This JS Fiddle i found in another question provides some coverage of the topic, but i can't seem to make it work for images, it only works for text and any changes i try just seem to break it or make the object not work or even appear. 我在另一个问题中发现的这个JS Fiddle提供了该主题的一些覆盖范围,但我似乎无法使其适用于图像,它仅适用于文本,并且我尝试进行的任何更改似乎都破坏了它或使对象不起作用或甚至出现。
Any help with this would be greatly appreciated as i have been trying different methods for a few weeks. 对此的任何帮助将不胜感激,因为我已经尝试了数周的不同方法。
Thanks in Advance 提前致谢
You can use :hover
in css. 您可以在CSS中使用
:hover
。
https://jsfiddle.net/pmy74ox5/6/ https://jsfiddle.net/pmy74ox5/6/
In your case (without js), use background-url in img
and change its url in img:hover
在您的情况下(不使用js),请在
img
使用background-url并在img:hover
更改其url
you could wrap the button in an empty anchor tag and then use the :active selector on the a tag. 您可以将按钮包装在一个空的锚标签中,然后在标签上使用:active选择器。
a:active{
//some styling
}
check out the selectors for a tags here: 在此处查看标签的选择器:
http://www.w3.org/TR/selectors/#the-user-action-pseudo-classes-hover-act http://www.w3.org/TR/selectors/#the-user-action-pseudo-classes-hover-act
the other way of doing it may be to use the checkbox hack: https://css-tricks.com/the-checkbox-hack/ 另一种方法是使用复选框hack: https : //css-tricks.com/the-checkbox-hack/
or in js, something like: 或在js中,类似:
var my_button = getElementById('button')
var clicked = false;
var buttonClicked = function(e){
if(!clicked){
my_button.classList.add("downClass");
}else{
my_button.classList.remove("downClass");
}
}
my_button.addEventListener('click',buttonClicked);
You can define the img in css and use a little jquery to have the desired effect : 您可以在css中定义img并使用一些jquery获得所需的效果:
.button{
background:url(https://cdn.tutsplus.com/vector/uploads/legacy/tuts/000-2011/463-steel-buttons/35.jpg) no-repeat 0 top;
width: 300px;
height: 300px;
display: inline-block;
}
.button.click{
background:url(https://cdn.tutsplus.com/vector/uploads/legacy/tuts/000-2011/463-steel-buttons/35.jpg) no-repeat -300px top;
}
here the js 这是js
$('.button').on("mousedown", function(){
$(this).addClass('click');
})
$('.button').on("mouseup", function(){
$(this).removeClass('click');
})
https://jsfiddle.net/pmy74ox5/7/ https://jsfiddle.net/pmy74ox5/7/
a solution with only jquery is possible too. 也只有jquery的解决方案也是可能的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.