简体   繁体   中英

Is there a way to resize images based on if they are vertical or horizontal using cycle2?

I want to resize the picture so the whole picture is always visible without scroll bars. Landscape pictures should go all the way to the edges, while vertical pictures should have a margin on the side, also, all pictures should be centered.

All the images have slightly different heights and widths. I am testing it with just 4 pictures, but I later plan to add a lot more.

This is what I have so far:

...
<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) check if width is greater than height

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) Either

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

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

or same as above to desired % width for non landscape

You can use jQuery like $(element).css( "width", "100%" ); to go 100% width of container or browser w/e

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM