手风琴的自动宽度

时间:2012-07-30 12:08:57

标签: jquery css width accordion

这是我用于水平手风琴的jquery代码。一切正常,但在这段代码中,主div宽度固定为100%(我希望这是自动宽度)。你能说出来吗?

(function(jQuery) {
    $('#accordion-1').easyAccordion({ 
        autoStart: false, 
        slideInterval: 3000
    jQuery.fn.easyAccordion = function(options) {

    var defaults = {            
        slideNum: false,
        autoStart: false,
        slideInterval: 3000
    };

    this.each(function() {

        var settings = jQuery.extend(defaults, options);        
        jQuery(this).find('dl').addClass('easy-accordion');


        // -------- Set the variables ------------------------------------------------------------------------------

        jQuery.fn.setVariables = function() {
            dlWidth = jQuery(this).width();
            dlHeight = jQuery(this).height();
            dtWidth = jQuery(this).find('dt').outerHeight();
            if (jQuery.browser.msie){ dtWidth = $(this).find('dt').outerWidth();}
            dtHeight = dlHeight - (jQuery(this).find('dt').outerWidth()-jQuery(this).find('dt').width());
            slideTotal = jQuery(this).find('dt').size();
            ddWidth = dlWidth - (dtWidth*slideTotal) - (jQuery(this).find('dd').outerWidth(true)-jQuery(this).find('dd').width());
            ddHeight = dlHeight - (jQuery(this).find('dd').outerHeight(true)-jQuery(this).find('dd').height());
        };
        jQuery(this).setVariables();


        // -------- Fix some weird cross-browser issues due to the CSS rotation -------------------------------------

        if (jQuery.browser.safari){ var dtTop = (dlHeight-dtWidth)/2; var dtOffset = -dtTop;  /* Safari and Chrome */ }
        if (jQuery.browser.mozilla){ var dtTop = dlHeight - 20; var dtOffset = - 20; /* FF */ }
        if (jQuery.browser.msie){ var dtTop = 0; var dtOffset = 0; /* IE */ }


        // -------- Getting things ready ------------------------------------------------------------------------------

        var f = 1;
        jQuery(this).find('dt').each(function(){
            jQuery(this).css({'width':dtHeight,'top':dtTop,'margin-left':dtOffset});    
            if(settings.slideNum == true){
                jQuery('<span class="slide-number">'+0+f+'</span>').appendTo(this);
                if(jQuery.browser.msie){    
                    var slideNumLeft = parseInt(jQuery(this).find('.slide-number').css('left')) - 14;
                    jQuery(this).find('.slide-number').css({'left': slideNumLeft})
                    if(jQuery.browser.version == 6.0 || jQuery.browser.version == 7.0){
                        jQuery(this).find('.slide-number').css({'bottom':'auto'});
                    }
                    if(jQuery.browser.version == 8.0){
                    var slideNumTop = jQuery(this).find('.slide-number').css('bottom');
                    var slideNumTopVal = parseInt(slideNumTop) + parseInt(jQuery(this).css('padding-top'))  - 12; 
                    jQuery(this).find('.slide-number').css({'bottom': slideNumTopVal}); 
                    }
                } else {
                    var slideNumTop = jQuery(this).find('.slide-number').css('bottom');
                    var slideNumTopVal = parseInt(slideNumTop) + parseInt(jQuery(this).css('padding-top')); 
                    jQuery(this).find('.slide-number').css({'bottom': slideNumTopVal}); 
                }
            }
            f = f + 1;
        });

        if(jQuery(this).find('.active').size()) { 
            jQuery(this).find('.active').next('dd').addClass('active');
        } else {
            jQuery(this).find('dt:first').addClass('active').next('dd').addClass('active');
        }

        jQuery(this).find('dt:first').css({'left':'0'}).next().css({'left':dtWidth});
        jQuery(this).find('dd').css({'height':ddHeight});    


        // -------- Functions ------------------------------------------------------------------------------

        jQuery.fn.findActiveSlide = function() {
                var i = 1;
                this.find('dt').each(function(){
                if(jQuery(this).hasClass('active')){
                    activeID = i; // Active slide
                } else if (jQuery(this).hasClass('no-more-active')){
                    noMoreActiveID = i; // No more active slide
                }
                i = i + 1;
            });
        };

        jQuery.fn.calculateSlidePos = function() {
            var u = 2;
            jQuery(this).find('dt').not(':first').each(function(){    
                var activeDtPos = dtWidth*activeID;
                if(u <= activeID){
                    var leftDtPos = dtWidth*(u-1);
                    jQuery(this).animate({'left': leftDtPos});
                    if(u < activeID){ // If the item sits to the left of the active element
                        jQuery(this).next().css({'left':leftDtPos+dtWidth});    
                    } else{ // If the item is the active one
                        jQuery(this).next().animate({'left':activeDtPos});
                    }
                } else {
                    var rightDtPos = dlWidth-(dtWidth*(slideTotal-u+1));
                    jQuery(this).animate({'left': rightDtPos});
                    var rightDdPos = rightDtPos+dtWidth;
                    jQuery(this).next().animate({'left':rightDdPos});    
                }
                u = u+ 1;
            });


        };

        jQuery.fn.activateSlide = function() {
            this.parent('dl').setVariables();    
            this.parent('dl').find('dd').css({'display':'block'});
            this.parent('dl').find('dd.plus').removeClass('plus');
            this.parent('dl').find('.no-more-active').removeClass('no-more-active');
            this.parent('dl').find('.active').removeClass('active').addClass('no-more-active');
            this.addClass('active').next().addClass('active');    
            this.parent('dl').findActiveSlide();
            if(activeID < noMoreActiveID){
                this.parent('dl').find('dd.no-more-active').addClass('plus');
            }
            this.parent('dl').calculateSlidePos();    
        };

        jQuery.fn.rotateSlides = function(slideInterval, timerInstance) {
            var accordianInstance = jQuery(this);
            timerInstance.value = setTimeout(function(){accordianInstance.rotateSlides(slideInterval, timerInstance);}, slideInterval);
            jQuery(this).findActiveSlide();
            var totalSlides = jQuery(this).find('dt').size();
            var activeSlide = activeID;
            var newSlide = activeSlide + 1;
            if (newSlide > totalSlides) newSlide = 1;
            jQuery(this).find('dt:eq(' + (newSlide-1) + ')').activateSlide(); // activate the new slide
        }


        // -------- Let's do it! ------------------------------------------------------------------------------

        function trackerObject() {this.value = null}
        var timerInstance = new trackerObject();

        jQuery(this).findActiveSlide();
        jQuery(this).calculateSlidePos();

        if (settings.autoStart == true){
            var accordianInstance = jQuery(this);
            var interval = parseInt(settings.slideInterval);
            timerInstance.value = setTimeout(function(){
                accordianInstance.rotateSlides(interval, timerInstance);
                }, interval);
        } 

        jQuery(this).find('dt').not('active').click(function(){        
            jQuery(this).activateSlide();
            clearTimeout(timerInstance.value);
        });    

        if (!(jQuery.browser.msie && jQuery.browser.version == 6.0)){ 
            jQuery('dt').hover(function(){
                jQuery(this).addClass('hover');
            }, function(){
                jQuery(this).removeClass('hover');
            });
        }
    });
    };
})(jQuery);​

这是html

    <div id="accordion-1">
                <dl>
                    <dt>First slide</dt>
                    <dd><h2>This is the first slide</h2><p></dd>
                    <dt>Second slide</dt>
                    <dd><h2>Here is the second slide</h2></dd>
                    <dt>One more slide</dt>
                    <dd><h2>One more slide to go here</h2><p></dd>
                    <dt>Another slide</dt>
                    <dd><h2>Another slide to go here</h2><p></dd>                                   
              </dl>
</div>

CSS

.easy-accordion{display:block;position:relative;overflow:hidden;padding:0;margin:0; width:auto}
.easy-accordion dt,.easy-accordion dd{margin:0;padding:0}
.easy-accordion dt,.easy-accordion dd{position:absolute}
.easy-accordion dt{margin-bottom:0;margin-left:0;z-index:5;/* Safari */ -webkit-transform: rotate(-90deg); /* Firefox */ -moz-transform: rotate(-90deg);-moz-transform-origin: 20px 0px;  /* Internet Explorer */ filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);cursor:pointer;}
.easy-accordion dd{z-index:1;opacity:0;overflow:hidden}
.easy-accordion dd.active{opacity:1;}
.easy-accordion dd.no-more-active{z-index:2;opacity:1}
.easy-accordion dd.active{z-index:3}
.easy-accordion dd.plus{z-index:4}
.easy-accordion .slide-number{position:absolute;bottom:0;left:10px;font-weight:normal;font-size:1.1em;/* Safari */ -webkit-transform: rotate(90deg); /* Firefox */ -moz-transform: rotate(90deg);  /* Internet Explorer */ filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=1);}

#accordion-1{ height:110px; background:#bababa; border-radius:8px; width:auto}
    #accordion-1 dl{height:110px; width:auto}   
    #accordion-1 dt{  text-align:left;padding:8px 15px 8px 10px;
    font:bold 12px Myriad Pro, Verdana, Geneva, sans-serif; color:#2c3e52;
    background-color: #B3B3B3;
    background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#BEBEBE), to(#B3B3B3));
    background: -moz-linear-gradient(top, #BEBEBE 0%, #B3B3B3 100%);
    border-radius:8px;
     box-shadow: 0px -2px 3px #616161;  
    }
    #accordion-1 dt.active{cursor:pointer; background:#bababa}
    #accordion-1 dt.hover{background-color:#bababa}
    #accordion-1 dd{
        padding:15px; background-color:#f3f3f3; border-left:0; margin:5px 0px; border-radius:6px 0px 0px 6px;
        -webkit-box-shadow: inset 0px 2px 2px #9b9b9b; box-shadow: inset 0px 2px 2px #9b9b9b; 

    }

2 个答案:

答案 0 :(得分:0)

首先改变

(function(jQuery) {

(function($) {

这允许在包装函数中使用$。您应该使用$代替jQuery

对于你的问题:这是你的浏览器为div设置100%。 Div总是扩展到你放置它们的元素的全宽。

旁注:将div设为100%宽度与保留默认宽度略有不同。这可能会让人感到有些困惑,所以我不会详细介绍。如果您想了解更多信息,请参阅:http://www.quirksmode.org/css/box.html

答案 1 :(得分:0)

有点晚了你应该将你的手风琴div放在另一个div容器中,然后将你的css应用到这个容器上。