mootools用按钮上的css效果切换div

时间:2016-02-26 04:55:25

标签: javascript toggle mootools

我有以下脚本来显示/隐藏一些文字。我想让切换按钮成为单击时更改的图像。 (例如,崩溃时为“+ Title 1”,扩展时为“ - Title 1”)有没有办法实现此目的?

window.addEvent( 'domready', function(){
$$( '.moreInfoWrapper' ).each(function(item){
    var thisSlider = new Fx.Slide( item.getElement( '.moreInfo' ), { duration: 500 } );
    thisSlider.hide();
    item.getElement( '.divToggle' ).addEvent( 'click', function(){ thisSlider.toggle(); } );
} );
} );

HTML:

<div class='moreInfoWrapper'>
    <div class='divToggle'>
        <h3>Title 1</h3>
    </div>
    <div class='moreInfo'>
        <h3>More Info About Item 1</h3>
        <p>Here is some content.</p>
        <p>More Content</p>
        <p>End of Content</p>
    </div>
</div>
<div class='moreInfoWrapper'>
    <div class='divToggle'>
        <h3>Title 2</h3>
    </div>
    <div class='moreInfo'>
        <h3>More Info About Item 2</h3>
        <p>Here is some content.</p>
        <p>More Content</p>
        <p>End of Content</p>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

它可以是文本上的(或者可以改变CSS类)。这有点笨拙的解决方案,但它的工作原理。也许有人回应得更好。

$$('.moreInfoWrapper .moreInfo').each(function(item){
    item.set('slide', { duration: 500 }).slide('hide');
});

$$('.moreInfoWrapper .divToggle').each(function(item){
  item.addEvent('click', function(e){
    var el = this.getFirst('h3');
    if (this.getNext('div').getDimensions().height == 0) {
      el.set('html', el.get('html').replace('+','-'));
    } else {
      el.set('html', el.get('html').replace('-','+'));
    }
    this.getNext('div').getFirst('.moreInfo').slide('toggle');
  });
});

Ty it on this jsfiddle