Dharma Records

A record of Ānandajoti’s publication work.

Menu
  • Notices
  • Talks
  • Texts
  • Audio
  • Video
  • Photos
  • Ecology
  • Culture
  • Archives
  • About
Menu

Resize images to Fit Browser Window

Posted on January 7, 2013January 2, 2013 by Ānandajoti

Yesterday I mentioned that I had just introduced the WOW-slider to my Photo Dharma website. From a developer’s point of view there is one big drawback with it, which is that you must fix the size of the photo first (e.g. 800px x 600px). The problem with that is we don’t know the size of the browser window people will be using, so it may be too small, or too big.

To get round that problem I wrote a new and fairly simple function, which first looks at the viewport, and then resizes the photograph to find the best fit for the screen. This is the javascript code for it, which I have annotated in the hope that it may help someone else.

Note that the script will only work if all the photos are the same aspect ratio. The ratio presumed here is 4×3 (1.33:1). The code can be adjusted for other ratios easy enough.

unction getViewport() {
// set aspect ratios here
    var lengthRatio = 2.12 // i.e 212 divided by 100
    var widthRatio = .4717 // i.e 100 divided by 212
// leave this as it is
    var viewPortHeight;
    var viewPortWidth;
// first get viewport width and height
    if (typeof window.innerWidth != 'undefined') {
      viewPortHeight = window.innerHeight;
      viewPortWidth = window.innerWidth;
// working with the aspect ratio (here 2.12:1) work out whether 
// it is better to work with the width or height
      if (viewPortHeight * lengthRatio < viewPortWidth) {
// if height if better, give a small margin at the bottom (here -30)
        viewPortHeight = window.innerHeight - 30;
        viewPortWidth = viewPortHeight * lengthRatio;
// updates some styles and center it, I don't know why but setting left and right to auto doesn't work
       document.getElementById('wowslider-container').style.position = "absolute";
      document.getElementById('wowslider-container').style.left = "7.5%";
      document.getElementById('wowslider-container').style.right = "7.5%";
      } else {
// if width is better
        viewPortWidth = window.innerWidth;
        viewPortHeight = viewPortWidth * widthRatio;
// updates some styles and center it
      document.getElementById('wowslider-container').style.position = "absolute";
      document.getElementById('wowslider-container').style.left = "0";
      }
// update the sizes, position and visibility now
      document.getElementById('wowslider-container').style.height = viewPortHeight + "px";
      document.getElementById('wowslider-container').style.width = viewPortWidth + "px";
      document.getElementById('wowslider-container').style.visibility = "visible";
}
  }

It is then possible to run the function with a call from body onload:

<body onload="getViewport()">

Examples can be seen here (click on Play Moving Slideshow):

  • Best of Cambodia
  • Best of Indonesia
  • Best of Malaysia
  • Best of Myanmar
  • Best of Thailand

another implementation which is particularly effective is on wide angle landscapes like these:

  • Landscapes and Skyscapes
  • The Lake Gardens

where the ratio of the photographs is 212:100

I hope this will help someone else out there who is grappling with the same problem.

Leave a Reply

Your email address will not be published. Required fields are marked *

QR code

SHORTLINK

Donations

there are many expenses involved in the making of this website. Even a small donation really helps to maintain and expand the site.

Recent Posts

  • Henri van Zeyst’s Contributions to the Sri Lankan Encyclopedia of Buddhism
  • The Reliefs of Buddhavanam eBook Published
  • Publication of the Pali Text Society’s Pali-English Dictionary
  • Dictionary of Pāli Proper Names Online
  • Photographs from the Ho Sian Temple in Taiping, Malaysia
  • New Book: The Reliefs of Buddhavanam
  • Photographs from Phanigiri Hilltop Monastery
  • Photos from Buddhavanam, Buddha’s Grove, 2
  • Photos from Buddhavanam, Buddha’s Grove, 1
  • My Big Fail: Kanaganahalli

Top Ten Tags

Buddha

Dhamma

Sangha

India

China

Indonesia

Thailand

Temples

Wisdom

Ethics

Other Websites

for my other websites please see my
LINKTREE

Sponsorship

 hosting sponsored by exabytes.my 

Random Posts

  • A Summary of the Mahavastu Published
  • Thirty Things that Invariably Happen with the Buddhas
  • BWV 021: Three Things leading to Success
  • Massive Complex at Muara Jambi, Sumatra
  • The True Size Of Africa
  • The Beginnings of Buddhist Art by A. Foucher – IV
  • BWV 004: Eight things Leading to Prosperity
  • Jātaka Commentary in Pāli, Version 2
  • The Life of the Buddha and his Disciples
  • BWV 053: Friendship knows no Boundaries

Recent Posts

  • Henri van Zeyst’s Contributions to the Sri Lankan Encyclopedia of Buddhism
  • The Reliefs of Buddhavanam eBook Published
  • Publication of the Pali Text Society’s Pali-English Dictionary
  • Dictionary of Pāli Proper Names Online
  • Photographs from the Ho Sian Temple in Taiping, Malaysia
  • New Book: The Reliefs of Buddhavanam
  • Photographs from Phanigiri Hilltop Monastery
  • Photos from Buddhavanam, Buddha’s Grove, 2
  • Photos from Buddhavanam, Buddha’s Grove, 1
  • My Big Fail: Kanaganahalli

Related Posts:

  • Pan and Zoom style Slidehows on the Photo Dharma website
  • Pan and Zoom Slideshows now on all pages on the…
  • MS Word: Split Windows Vertically
  • Download sets of Photos on Photo Dharma
  • Poster on Impermanence: FastStone Image Viewer
  • Easy Reading in your Browser: iReader for Firefox
  • Lazy Loading on Photo Dharma
  • Facebook Comments: Dynamic Urls on Static Pages
  • Making short urls using javascript and a .htaccess file
  • New Design and Upgrade of Photo Dharma Website
© 2025 Dharma Records | Powered by Minimalist Blog WordPress Theme