【问题标题】:Collapsible div height issue on ClickClick上的可折叠div高度问题
【发布时间】:2018-05-20 16:54:40
【问题描述】:

我遇到了一个关于可折叠 div 高度的奇怪问题。即使在 div 关闭/折叠后,div 的展开高度仍然存在。请参考下图

Javascript 在点击时将高度添加到 li。作为参考,这里是检查元素部分。

<ul id="og-grid" class="og-grid">

            <li><a target="_blank" href="www.somewebsite.com" data- 
   largesrc="uploadimage/project/sfs.jpg" data-title="Website Redesign of " 
   data-type="WebSite" data-description="Website Redesign "><img 
   src="uploadimage/project/thumb/sfs.jpg" alt="img00"></a></li>

   <li style="**transition: height 350ms ease 0s; height: 719px;**" 
 class="">
   <a href="#/" data-largesrc="uploadimage/project/notouch.jpg" data- 
  title="No Touch" data-type="Mobile Application" data-description="Notouch 
  is a simple but useful app that helps you to lock the screen temporarily 
  to prevent unauthorized clicks or to go to other apps from the current 
  screen. This is especially useful when you give your Phone to kids to view 
  videos or YouTube. This app will disable the touch facility temporarily."> 
  <img src="uploadimage/project/thumb/notouch.jpg" alt="img01"></a></li>

  <li><a target="_blank" href="www.fhjgh.com" data- 
  largesrc="uploadimage/project/2capture.jpg" data-title="Test" data- 
  type="WebSite" data-description="hI there njwehkasd kahffhsaf sdkfhsdf 
  saklhflkdshf sdhfksdhf kldsh"><img 
  src="uploadimage/project/thumb/2capture.jpg" alt="img02"></a></li>

  <li><a target="_blank" href="http://www.jhghjghjg.in" data- 
  largesrc="uploadimage/project/green.jpg" data-title="Green Homes" data- 
  type="WebSite" data-description=", due to better usage of pictures"><img 
  src="uploadimage/project/thumb/green.jpg" alt="img03"></a></li>

 <li><a href="#/" data-largesrc="uploadimage/project/leonardo.jpg" data- 
 title="Leonardo" data-type="Logo Design" data-description="The Logo design 
 . We have used the negative space design concept here to make the L hidden 
  inside a box ( Box represent a furniture)"><img 
 src="uploadimage/project/thumb/leonardo.jpg" alt="img04"></a></li>

</ul>

Javascript 部分(我相信)就是这个。

  open: function () {

        setTimeout($.proxy(function () {
            // set the height for the preview and the item
            this.setHeights();
            // scroll to position the preview in the right place
            this.positionPreview();
        }, this), 25);

    },
    close: function () {

        var self = this,
            onEndFn = function () {
                if (support) {
                    $(this).off(transEndEventName);
                }
                self.$item.removeClass('og-expanded');
                self.$previewEl.remove();
            };

        setTimeout($.proxy(function () {

            if (typeof this.$largeImg !== 'undefined') {
                this.$largeImg.fadeOut('fast');
            }
            this.$previewEl.css('height', 0);
            // the current expanded item (might be different from this.$item)
            var $expandedItem = $items.eq(this.expandedIdx);
            $expandedItem.css('height', $expandedItem.data('height')).on(transEndEventName, onEndFn);

            if (!support) {
                onEndFn.call();
            }

        }, this), 25);

        return false;

    },
    calcHeight: function () {

        var heightPreview = winsize.height - this.$item.data('height') - marginExpanded,
            itemHeight = winsize.height;

        if (heightPreview < settings.minHeight) {
            heightPreview = settings.minHeight;
            itemHeight = settings.minHeight + this.$item.data('height') + marginExpanded;
        }

        this.height = heightPreview;
        this.itemHeight = itemHeight;

    },
    setHeights: function () {

        var self = this,
            onEndFn = function () {
                if (support) {
                    self.$item.off(transEndEventName);
                }
                self.$item.addClass('og-expanded');
            };

        this.calcHeight();
        this.$previewEl.css('height', this.height);
        this.$item.css('height', this.itemHeight).on(transEndEventName, onEndFn);

        if (!support) {
            onEndFn.call();
        }

    },
    positionPreview: function () {

        // scroll page
        // case 1 : preview height + item height fits in window´s height
        // case 2 : preview height + item height does not fit in window´s height and preview height is smaller than window´s height
        // case 3 : preview height + item height does not fit in window´s height and preview height is bigger than window´s height
        var position = this.$item.data('offsetTop'),
            previewOffsetT = this.$previewEl.offset().top - scrollExtra,
            scrollVal = this.height + this.$item.data('height') + marginExpanded <= winsize.height ? position : this.height < winsize.height ? previewOffsetT - (winsize.height - this.height) : previewOffsetT;

        $body.animate({ scrollTop: scrollVal }, settings.speed);

    },
    setTransition: function () {
        this.$previewEl.css('transition', 'height ' + settings.speed + 'ms ' + settings.easing);
        this.$item.css('transition', 'height ' + settings.speed + 'ms ' + settings.easing);
    },
    getEl: function () {
        return this.$previewEl;
    }
}

return {
    init: init,
    addItems: addItems
};

})();

请指教,如何解决这个问题。 (是否有 CSS 或 Javascript 方式)

【问题讨论】:

    标签: javascript html css height


    【解决方案1】:

    尝试在动画结束时在包含打开图像的容器上执行命令 javascript: remove() 或 empty()。如果您在尝试这些命令后仍然看到错误的高度,这意味着您已经在容器上应用了宽度/高度属性,因此您必须删除它们,将它们的值更改为 ''。

    编辑:

    close: function () {
        ....
        setTimeout($.proxy(function () {            
            document.getElementsByClassName('og-expander')[0].parentElement.removeAttribute("style");
            if (typeof this.$largeImg !== 'undefined') {
        ....
    

    我已经测试过了,它可以工作。

    【讨论】:

    • JS 正在对点击的 li (item) 应用内联样式。我认为,这实际上是导致问题的原因。
    • 。但是,一旦单击关闭按钮或单击图像(切换),同样不会被删除
  • 查看您输入的示例,在我看来,由于 css 存在问题。删除应用于容器的样式和类属性应该可以解决问题,当然最后都是。无法查看实际页面的 css,通过删除剩余属性使用 javascript 进行补救。
  • 谢谢。请在此处找到链接(我的一个网站test.viawebindia.com/portfolio
  • 我看到关闭窗口从
  • 中删除了“og-expander”类,正确,但您还必须删除样式属性,尝试在从
  • 中删除类之前插入> removeAttribute(“样式”)。
  • 抱歉,我没有正确理解您。你能解释一下吗?这行代码似乎也将内联样式应用于项目。
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签