简体   繁体   English

CSS 过渡不起作用 onclick

[英]CSS transition not working onclick

I just want to show a notification alert on click in bottom left in my page.我只想在我的页面左下角点击时显示通知警报。 The notification box appears correctly but transition isn't working.通知框显示正确,但转换不起作用。 I tried this way.我试过这种方式。

 #notification-alert { display: none; font-family: 'Montserrat-regular'; color: white; bottom: 20px; right: 20px; position: fixed; background-color: #f4b251; border-bottom: 4px solid #E89F3C; color: #fff; padding: 20px; border-radius: 14px; transition-timing-function: cubic-bezier(0.175, 0.885, 0.32, 1.275); transition-duration: 500ms; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type='submit' onclick='$("#notification-alert").show()'> <div id="notification-alert"> <span>Great job, you're under way!</span> </div>

Here is the fiddle What's wrong with the code?这是小提琴代码有什么问题?

Here's the working result.这是工作结果。

 var notificationTimer = 2000; // miliseconds to hide the notification alert $('.submit-button').on('click', function () { $('.notification-alert').addClass('notification-alert--shown'); setTimeout(function () { $('.notification-alert').removeClass('notification-alert--shown'); }, notificationTimer) });
 .notification-alert { color: white; bottom: 20px; right: 20px; position: fixed; background-color: #f4b251; border-bottom: 4px solid #E89F3C; color: #fff; padding: 20px; border-radius: 14px; transition-timing-function: cubic-bezier(0.175, 0.885, 0.32, 1.275); transform: translateX(100%); transition: all 500ms; opacity: 0; z-index: -1; } .notification-alert--shown { opacity: 1; transform: translateX(0); transition: all 500ms; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="submit-button">show notification</button> <div class="notification-alert"> <span>Great job, you're under way!</span> </div>

I changed a few things:我改变了几件事:

  1. I changed notification-alert to a class because it's good practice to use classes for CSS transitions because the classes represent different states that your element will transition to and from.我将notification-alert更改为一个类,因为使用类进行 CSS 转换是一种很好的做法,因为这些类表示元素将要转换的不同状态。
  2. Additional, I added a new class notification-alert--shown which represents the state of this element when it is shown.另外,我添加了一个新的类notification-alert--shown ,它表示该元素在显示时的状态。
  3. I used jquery to add the class notification-alert--shown when the button is clicked to initiate the transition.我使用 jquery 添加类notification-alert--shown当单击按钮启动转换时显示。

Now we need to explain how CSS transitions work:现在我们需要解释 CSS 过渡是如何工作的:

CSS transitions work by adding frames to and from different CSS classes (as a good practice). CSS 过渡通过在不同 CSS 类之间添加帧来工作(作为一种很好的做法)。 In order to make a transition happen, you need at least two classes.为了实现转换,您至少需要两个类。 In this case, that's notification-alert and notification-alert--shown .在这种情况下,就是notification-alertnotification-alert--shown These classes only differ (ie have conflicting properties) with the properties transform and opacity .这些类仅与属性transformopacity不同(即具有冲突的属性)。

Since those properties are different between the two classes, I can tell CSS to transition (that is, add frames between) the two classes when they change.由于这两个类之间的这些属性不同,我可以告诉 CSS 在两个类发生变化时进行转换(即,在它们之间添加帧)。

So when I add the class notification-alert--shown , I am telling the browser to add frames to transition from the state {opacity: 0; transform: translateX(100%);}因此,当我添加类notification-alert--shown ,我告诉浏览器添加帧以从状态{opacity: 0; transform: translateX(100%);} {opacity: 0; transform: translateX(100%);} to the new state {opacity: 1; transform: translateX(0);} {opacity: 0; transform: translateX(100%);}到新状态{opacity: 1; transform: translateX(0);} {opacity: 1; transform: translateX(0);}

And I tell the browser that I want to transition all the different property by adding all to transition: all ... .我告诉浏览器我想通过将all添加到transition: all ...来转换所有不同的属性。


You also brought up a few questions in the comments:您还在评论中提出了一些问题:

This working great.这很好用。 But it's only show fade effect.但它只显示淡入淡出效果。 But why this kind of animation isn't working?transition-timing-function: cubic-bezier(0.175, 0.885, 0.32, 1.275);但是为什么这种动画不起作用?transition-timing-function:cubic-bezier(0.175, 0.885, 0.32, 1.275);

And this leads me to think you have a slight misunderstanding about the transition-timing-function property.这让我觉得你对transition-timing-function属性有一点误解。 The transition-timing-function is not associated with how the element move exactly--it is only concerned with how the transition you asked for is applied over time. transition-timing-function与元素如何精确移动无关——它只关心你要求的过渡是如何随着时间的推移而应用的。 You can read more about transition timing property here .您可以在此处阅读有关转换时间属性的更多信息。


And you also asked for one more thing:你还要求一件事:

Looks great now!现在看起来很棒! How can I fade out after few seconds?如何在几秒钟后淡出?

To do that, you can use window.setTimeout to run some code to remove the class after a certain amount of time.为此,您可以使用window.setTimeout运行一些代码以在一定时间后删除该类。

I've updated the example to demonstrate.我已经更新了示例以进行演示。


Here is a good tutorial on CSS transitions on CSS-tricks .这是一个关于 CSS-tricks 上的 CSS 转换的好教程

You can't transition display property, it's either visible or not, no intermediate steps.你不能转换显示属性,它要么可见,要么不可见,没有中间步骤。 However, you can easily achieve needed visual effect with animated opacity:但是,您可以使用动画不透明度轻松实现所需的视觉效果:

 function showAlert () { var $alert = $("#notification-alert").show() setTimeout(function() { $alert.addClass("show") }) }
 #notification-alert { display: none; opacity: 0; /* <---- additional opacity */ color: white; bottom: 20px; right: 20px; position: fixed; background-color: #f4b251; border-bottom: 4px solid #E89F3C; color: #fff; padding: 20px; border-radius: 14px; transition-property: opacity; transition-timing-function: cubic-bezier(0.175, 0.885, 0.32, 1.275); transition-duration: 500ms; } #notification-alert.show { opacity: 1; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="submit" onclick="showAlert()"> <div id="notification-alert"> <span>Great job, you're under way!</span> </div>

Note, that you need to schedule animation phase to be run in the next rendering cycle, for this you can set a delay with setTimeout .请注意,您需要安排动画阶段在下一个渲染周期中运行,为此您可以使用setTimeout设置延迟。

Finally, why use both display: none and opacity: 0 ?最后,为什么同时使用display: noneopacity: 0 Well, we don't want user to click "invisible" alert by accident, which would happen if you only used opacity .好吧,我们不希望用户意外单击“不可见”警报,如果您只使用opacity就会发生这种情况。 So we also need to really hide alert with display: none (or some other way, like negative position).因此,我们还需要真正隐藏带有display: none警报(或其他方式,例如负位置)。

Transition is more like an animation.过渡更像是一个动画。

div.sicon a {
    background:-moz-radial-gradient(left, #ffffff 24%, #cba334 88%);
    transition: background 0.5s linear;
    -moz-transition: background 0.5s linear; /* Firefox 4 */
    -webkit-transition: background 0.5s linear; /* Safari and Chrome */
    -o-transition: background 0.5s linear; /* Opera */
    -ms-transition: background 0.5s linear; /* Explorer 10 */
}

So you need to invoke that animation with an action.所以你需要用一个动作来调用那个动画。

div.sicon a:hover {
    background:-moz-radial-gradient(left, #cba334 24%, #ffffff 88%);
}

Also check for browser support and if you still have some problem with whatever you're trying to do!还要检查浏览器是否支持,如果您尝试做的事情仍然有问题! Check css-overrides in your stylesheet and also check out for behavior: ***.htc css hacks.. there may be something overriding your transition!检查样式表中的 css-overrides 并检查behavior: ***.htc css hacks .. 可能有一些东西覆盖了您的过渡!

You should check this out: https://www.w3schools.com/css/css3_transitions.asp你应该看看这个: https : //www.w3schools.com/css/css3_transitions.asp

As far as I know, CSS transitions don't effect the display: none CSS property.据我所知,CSS 过渡不会影响display: none CSS 属性。 For this, you need to change your code as follows:为此,您需要按如下方式更改代码:

#notification-alert {
display: none;
font-family: 'Montserrat-regular';
color: white;
bottom: 20px;
right: 20px;
position: fixed;
background-color: #f4b251;
border-bottom: 4px solid #E89F3C;
color: #fff;
padding: 20px;
border-radius: 14px;
transition-timing-function: cubic-bezier(0.175, 0.885, 0.32, 1.275);
transition-duration: 500ms;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='submit' class="showme">
<div id="notification-alert">
  <span>Great job, you're under way!</span>
</div>
<script>
  $(".showme").on("click", function(){
  $("#notification-alert").fadeIn();
});
</script>
<script type="text/javascript">
        $('#alert').fadeIn("slow");setTimeout(function(){$("#alert").fadeOut(2000);},3000);
</script>

try this, change #alert to your alert id.试试这个,将#alert更改为您的警报 ID。

Update CSS like this:像这样更新 CSS:

#notification-alert{

  color: white;
  bottom: 20px;
  right: 20px;
  position: fixed;
  background-color: #f4b251;
  border-bottom: 4px solid #E89F3C;
  color: #fff;
  padding: 20px;
  border-radius: 14px;

}

And HTML like this:和这样的 HTML:

<input type='submit' onclick='$("#notification-alert").fadeIn(1000)'>

<div id="notification-alert" style="display:none;">
  <span>Great job, you're under way!</span>
</div>

You can achieve via jQuery:您可以通过 jQuery 实现:

Try this:尝试这个:

$("#notification-alert").css({"opacity" : "0"}); //put the opacity to 0
   $("#submit_btn").click(function(e){ //Create an identifier for the submit button
   $("#notification-alert").fadeTo(500, 1);
});

Also with JS, like this:也用JS,像这样:

 var button = document.getElementById('btn'); button.onclick = function() { var alertBlock = document.getElementById('notification-alert') alertBlock.classList.add('notification-alert-visible'); };
 #notification-alert { color: white; bottom: 20px; right: 20px; position: fixed; background-color: #f4b251; border-bottom: 4px solid #E89F3C; color: #fff; padding: 20px; border-radius: 14px; } .notification-alert-hidden { transition: all .5s ease-in-out; opacity: 0; } .notification-alert-visible { opacity: 1; }
 <input type='submit' id='btn'> <div id="notification-alert" class="notification-alert-hidden"> <span>Great job, you're under way!</span> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM