简体   繁体   English

如何在单击时更改图像并在使用HTML / CSS / JS发布后恢复图像

[英]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.

相关问题 如何单击一个按钮并再次单击它以关闭所有内容以及如何在 go 使用 HTML CSS 或 JS 进入暗模式时更改图像? - How to click on a button and click on it again to close all the content AND how to change image when you go into darkmode using HTML CSS or JS? 如何更改鼠标按住的图像,然后在释放时执行事件? - How do you change image on mouse hold then execute event upon release? 单击按钮后如何更改背景图像? - How do I change background image upon button click? 如何使用 Javascript function 更改 HTML 代码中的图像? - How do you change an image in HTML code using Javascript function? 如何通过CSS和JS每隔15秒左右更改我的背景图像 - How Do I Make My Background Image Change Every 15 Seconds Or So Via CSS & JS 使用JS或HTML更改单选按钮单击时的图像 - Change image on radio button click using JS or HTML 如何使我的图像 go 从旋转到直线(HTML,CSS,JS,) - How do I make my image go FROM rotated to straight (HTML, CSS, JS,) 如何使用Chart.js恢复xAxes? - How do you revert xAxes with Chart.js? 如何在 js 中不触发 onclick 的情况下单击 HTML 元素? - How do you click on an HTML element without triggering onclick in js? 使用Jquery,如何更改要单击的对象的CSS? - Using Jquery, how do you change the css of the object you are clicking?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM