Javascript slide effect onclick - Stack Overflow

I'd like to add a slide & fade effect to a DIV, with purely Javascript, using "onclick&qu

I'd like to add a slide & fade effect to a DIV, with purely Javascript, using "onclick".

The code is here: /

The DIV that has to slide has id="pulldown_contents_wrapper". This DIV is contained in a SPAN, that also triggers it:

<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:

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? (*I'd like to use only pure JS or the Mootols framework).

I have tried to implement the code from: why javascript onclick div slide not working? 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

    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?

I'd like to add a slide & fade effect to a DIV, with purely Javascript, using "onclick".

The code is here: http://jsfiddle/TCUd5/

The DIV that has to slide has id="pulldown_contents_wrapper". This DIV is contained in a SPAN, that also triggers it:

<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:

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? (*I'd like to use only pure JS or the Mootols framework).

I have tried to implement the code from: why javascript onclick div slide not working? 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

    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?

Share Improve this question edited May 23, 2017 at 12:04 CommunityBot 11 silver badge asked Oct 19, 2012 at 19:15 GeorgeGeorge 5404 gold badges8 silver badges23 bronze badges 4
  • 1 Why not use jQuery. It's also purely javascript, or am i missing something? – intelis Commented Oct 19, 2012 at 19:21
  • 1 I am working on SocialEngine 4, it does not uses jQuery by default...it uses Mootols – George Commented Oct 19, 2012 at 19:23
  • You can find a couple of demos on mootools – Hashem Qolami Commented Oct 19, 2012 at 19:26
  • Thanks, I have already looked at all the demos possible, I just don't have any idea how to implement something that would work with my code. – George Commented Oct 19, 2012 at 19:29
Add a ment  | 

1 Answer 1

Reset to default 3

jQuery is a lot easier, but with pure javascript you can do it.

In the CSS you'll need to use transitions

#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.

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

call the jQuery library, most easily done from the google hosting

<script type="text/javascript" src="http://ajax.googleapis./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. 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. 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744257358a4565463.html

相关推荐

  • Javascript slide effect onclick - Stack Overflow

    I'd like to add a slide & fade effect to a DIV, with purely Javascript, using "onclick&qu

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信