有没有办法根据使用cycle2是垂直还是水平来调整图像的大小?

时间:2017-07-26 22:52:11

标签: javascript jquery html css cycle2

我想调整图片大小,以便在没有滚动条的情况下整个图片始终可见。风景图片应该一直到边缘,而垂直图片边缘应该有边距,所有图片都应该居中。

所有图像的高度和宽度略有不同。我只用4张照片测试它,但我后来计划添加更多。

这是我到目前为止所做的:

...
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
<script src="js/jquery.cycle2.min.js"></script>
<style type="text/css">
  * {
    padding: 0;
    margin: 0;
  }

  body {
    font-family: sans-serif;
    display: block;
    max-height: 100%;
  }

  img {
    max-width: 100%; max-height: 100%; margin: 0;
  }

  .cycle-slideshow {
    max-width: 100%;
    max-height: 100%;

  }
</style>

<body>
  <div class="cycle-slideshow">
    <img src="photos/NYC_Skyline_Revised.jpg" alt="NYC Skyline Photo">
    <img src="photos/MinionDrawing-1.jpg" alt="NYC Skyline Photo">
    <img src="photos/NYC_Skyline_Daytime.jpg" alt="NYC Skyline Photo">
    <img src="photos/HeavenlySunrise-2.jpg" alt="Sunrise Photo">
  </div>
  <!-- jQuery first, then Tether, then Bootstrap JS. -->
  <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
</body>
...

1 个答案:

答案 0 :(得分:0)

1)检查宽度是否大于高度

How to get image size (height & width) using JavaScript?

var img = document.getElementById('imageid'); 
//or however you get a handle to the IMG
var width = img.clientWidth;
var height = img.clientHeight;

2)要么

How do I auto-resize an image to fit a div container

max-width:100%;
max-height:100%;

或与上述相同,非景观所需的%宽度

You can use jQuery,如$(element).css( "width", "100%" );,可以100%扩展容器或浏览器的宽度