如何使用 javascript 设置 DIV 可见加载

How to set a DIV visible onload with javascript

我有这个页面我想制作第一张卡片描述以显示onload。 div id"firstitem",我无法计算出 javascript 来显示(子)内容加载。

(function (global, factory) {
  if (typeof define === 'function' && define.amd) {
    define(['jquery'], factory);
  } else {
    factory(jQuery);
  }
})(this, function ($) {
  var defaultSettings = {
    prefix: "imagelistexpander-"
  };

  var waitForFinalEvent = (function () {
    var timer = null;
    return function (callback, uniqueId) {
      if (timer) {
        clearTimeout(timer);
      }
      timer = setTimeout(callback, 500);
    };
  })();

  var imageListExpander = function (list, _settings) {
    var
      settings = $.extend({}, defaultSettings, _settings),
      $list = $(list),
      $items = $list.find('.' + settings.prefix + 'item'),
      $trigger = $list.find('.' + settings.prefix + 'trigger'),
      $closeTrigger = $list.find('.' + settings.prefix + 'trigger-close'),

      initialize = function () {
        $(window).bind('resize', resizeWindow);
        $trigger.bind('click', clickItem);
        $closeTrigger.bind('click', clickCloseTrigger);
      },
      resizeWindow = function () {
        waitForFinalEvent(function () {
          $items.filter('.active').each(function () {
            var
              $item = $(this),
              $expanderContents = $item.find('.' + settings.prefix + 'expander-contents'),
              $expander = $item.find('.' + settings.prefix + 'expander'),

              expanderHeight = $expanderContents.outerHeight();

            $item.css(
              'height',
              $item.find('.' + settings.prefix + 'contents').outerHeight() + expanderHeight
            );

            $expander.css('max-height', expanderHeight);
          });
        });
      },
      clickItem = function () {
        var $item = $(this).parents('.' + settings.prefix + 'item');
        `enter code here`

        if ($item.hasClass('active')) {
          hideItems($item);
        } else {
          showItem($item);
        }
      },
      clickCloseTrigger = function () {
        hideItems($items);
      },
      showItem = function ($item) {
        hideItems($item.siblings());

        var
          $expanderContents = $item.find('.' + settings.prefix + 'expander-contents'),
          $expander = $item.find('.' + settings.prefix + 'expander'),

          expanderHeight = $expanderContents.outerHeight();

        $item.addClass('active').css(
          'height',
          $item.find('.' + settings.prefix + 'contents').outerHeight() + expanderHeight
        );

        $expander.css('max-height', expanderHeight);
      },
      hideItems = function ($targetItems) {
        $targetItems = $targetItems.filter('.active');

        var $expanders = $targetItems.find('.' + settings.prefix + 'expander');

        $targetItems.each(function () {
          var $item = $(this);
          $item.css(
            'height',
            $item.find('.' + settings.prefix + 'contents').outerHeight()
          );
        });

        $targetItems.removeClass('active');
        $expanders.css('max-height', 0);
      };

    initialize();
  };

  $.fn.imagelistexpander = function (settings) {
    $(this).each(function () {
      imageListExpander(this, settings || {});
    });
  };

  return $;
});

(function (global, $) {
  $('.gallery-items').imagelistexpander({
    prefix: "gallery-"
  });
})(this, jQuery)
.gallery-expander {
  position: absolute;
  left: 0;
  right: 0;
  overflow: hidden;
  max-height: 0;
  -webkit-transition: max-height 500ms ease;
  -o-transition: max-height 500ms ease;
  transition: max-height 500ms ease;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container-fluid">
  <ul class="gallery-items">
    <li class="gallery-item highlight-box ">
      <div class="gall">
        <div class="gallery-contents gallery-trigger hl-inner">
          <div class="thumbnail ">12121</div>
          <div class="title">Gallery Item</div>
        </div>
        <div class="gallery-expander" id="firstitem">
          <div class="gallery-expander-contents">
            <div class="gallery-trigger-close close">x</div>
            <div class="title">ytytytyt</div>
            <div class="contents">Lorem ipsum dolor</div>
          </div>
        </div>
      </div>
    </li>
  </ul>
</div>

只需在您的页面加载时编写这段代码,就可以了

  $('#firstitem').css('max-height',100);

您可以使用 vanilla JS 或 jQuery

收听 onload 事件

您想将 max-height 设置为其默认值以显示该元素,因为您使用 max-height: 0 的 CSS 是隐藏它的原因

香草 JS:

window.onload = () => {
  document.getElementById('firstitem').style.maxHeight = 'none';
};

jQuery:

$(window).on('load', () => {
  $('#firstitem').css('max-height', 'none');
});