当前位置: 首页 > 工具软件 > enquire.js > 使用案例 >

full-background.js

白君之
2023-12-01
$(window).on('load', function () {
    $(window).trigger('resize');
});

$(function () {
  var $window = $(window),
  isLoaded = false;

  function resizeBg() {
    var winWidth = $window.width(),
      winHeight = $window.height(),
      viewportAspectRatio = winWidth / winHeight;

    $('.full-bg').each(function (index, item) {
      var $bg = $(item),
      bgWidth = $bg.width(),
      bgHeight = $bg.height(),
      aspectRatio = bgWidth / bgHeight;

        if (viewportAspectRatio < aspectRatio ) {
          $bg.removeClass('full-width')
            .addClass('full-height')
            .css({
               top: 0,
               left: -(winHeight * aspectRatio - winWidth) / 2
             });
        } else {
          $bg.removeClass('full-height')
            .addClass('full-width')
            .css({
              top: -(winWidth / aspectRatio - winHeight) / 2,
              left: 0
            });
        }
    });
  }
  $window.on('resize', resizeBg);

  $('.full-bg').on('load', function () {
    $(window).trigger('resize');
  });
});

enquire.register('screen and (max-width: 760px)', {
  match: function () {
    $('.responsive-img').each(function () {
      var $img = $(this),
        src = $img.data('mobile-src');
      if (src) {
        $img.attr('src', src).show();
      } else {
        $img.hide();
      }
    });
  },
  unmatch: function () {
    $('.responsive-img').each(function () {
      var $img = $(this),
        src = $img.data('src');
      if (src) {
        $img.attr('src', src).show();
      } else {
        $img.hide();
      }
    });
  }
});

enquire.register('screen and (min-width: 761px)', {
  match: function () {
    $('.responsive-img').each(function () {
      var $img = $(this),
        src = $img.data('src');
      if (src) {
        $img.attr('src', src).show();
      } else {
        $img.hide();
      }
    });
  },
  unmatch: function () {
    $('.responsive-img').each(function () {
      var $img = $(this),
        src = $img.data('mobile-src');
      if (src) {
        $img.attr('src', src).show();
      } else {
        $img.hide();
      }
    });
  }
});

 

转载于:https://www.cnblogs.com/Brittany-yan/p/5279242.html

 类似资料: