简体   繁体   English

Javascript幻灯片效果onclick

[英]Javascript slide effect onclick

I'd like to add a slide & fade effect to a DIV, with purely Javascript, using "onclick". 我想使用“onclick”为纯粹的Javascript添加幻灯片和淡入淡出效果到DIV。

The code is here: http://jsfiddle.net/TCUd5/ 代码在这里: http//jsfiddle.net/TCUd5/

The DIV that has to slide has id="pulldown_contents_wrapper". 必须滑动的DIV具有id =“pulldown_contents_wrapper”。 This DIV is contained in a SPAN, that also triggers it: 此DIV包含在SPAN中,也会触发它:

<span onclick="toggleUpdatesPulldown(event, this, '4');" style="display: inline-block;" class="updates_pulldown" >
    <div class="pulldown_contents_wrapper" id="pulldown_contents_wrapper">

And I think the JS code that controls the SPAN onclick is: 我认为控制SPAN onclick的JS代码是:

var toggleUpdatesPulldown = function(event, element, user_id) {
  if( element.className=='updates_pulldown' ) {
    element.className= 'updates_pulldown_active';
    showNotifications();
  } else {
    element.className='updates_pulldown';
  }  
}

If it is not possible to make it with pure JS, do you have an idea how could I do it with Mootools? 如果不能用纯JS制作它,你知道我怎么能用Mootools做到这一点? (*I'd like to use only pure JS or the Mootols framework). (*我只想使用纯JS或Mootols框架)。

I have tried to implement the code from: why javascript onclick div slide not working? 我试图实现代码: 为什么javascript onclick div幻灯片不工作? but with no results. 但没有结果。

Thanks a lot. 非常感谢。

I have managed to make it with Mootools, but I can't figure it out how to add a slide & fade effect, and a delay on mouseout 我已经设法使用Mootools,但我无法弄清楚如何添加幻灯片和淡入淡出效果,以及延迟鼠标移出

    window.addEvent('domready', function() {
        $('updates_pulldown').addEvents({
          mouseenter: function(){
            $('updates_pulldown').removeClass('updates_pulldown').addClass('updates_pulldown_active')
            $('pulldown_contents_wrapper').set('tween', {
              duration: 1000,
              physics: 'pow:in:out',
              transition: Fx.Transitions.Bounce.easeOut // This could have been also 'bounce:out'
            }).show();
          },
          mouseleave: function(){
            $('pulldown_contents_wrapper').set('tween', {
                duration: 1000,
                delay: 1000,
                }).hide();
            $('updates_pulldown').removeClass('updates_pulldown_active').addClass('updates_pulldown')
          },
        });
    });

  var toggleUpdatesPulldown = function(event, element, user_id) {
      showNotifications();
  }

Any idea? 任何想法?

jQuery is a lot easier, but with pure javascript you can do it. jQuery更容易,但使用纯JavaScript可以做到这一点。

In the CSS you'll need to use transitions 在CSS中,您需要使用转换

#thing { position:relative;
  top: 0px;
  opacity: 0.8;
  -moz-transition: top 1s linear, opacity 1s linear;
  -webkit-transition: top 1s linear, opacity 1s linear;
 }

then in the javascript when you change the position of the element, it should change via the css transitions. 然后在javascript中当你改变元素的位置时,它应该通过css转换来改变。

var toggleUpdatesPulldown = function(event, element, user_id) {
  if( element.className=='updates_pulldown' ) {
     element.style.top = someValue; //something like '100px' will slide it down 100px
     element.style.opacity = '1.0'; //will fade the content in from 0.8 opacity to 1.0
     element.className= 'updates_pulldown_active';
    showNotifications();



EDIT - provided jQuery code 编辑 - 提供jQuery代码

call the jQuery library, most easily done from the google hosting 调用jQuery库,最容易从谷歌主机上完成

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>

make the hover function 制作悬停功能

$(document).ready(function() {
        $('.updates_pulldown').hover( //first function is mouseon, second is mouseout
            function() {
                $(this).animate({top: '50px'}).animate({opacity: '1.0'});
            },
            function() { //delay 1000 milliseconds, animate both position and opacity
                $(this).delay(1000).animate({top: '0px'}).animate({opacity: '0.5'});
            }
        )
    })

the function timing will be the same as whatever you set it to in the css with transition tags. 函数时序将与使用转换标记在css中设置的任何时间相同。 using 'this' instead of the class name again makes sure that the effect only occurs on the specific instance of the class that is hovered over. 再次使用'this'而不是类名确保效果仅发生在悬停的类的特定实例上。 im not sure if this animation is exactly what you were asking for, but if i understand the question correctly then the main functionality will work for you. 我不确定这个动画是否正是你所要求的,但如果我正确理解了这个问题,那么主要功能将对你有用。 just change the numbers and such to fit your needs. 只需更改数字等,以满足您的需求。

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

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