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

  • Dharma Storybooks on Photo Dharma
  • The Exalted Chapters, A Simplified Translation of the Udānavarga
  • Translation of the Sanskrit Udānavarga Published
  • The Lives of Inspiring Chinese Nuns
  • Five New Albums on Photo Dharma
  • 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

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

  • Maps of the Chinese Pilgrims’ Journeys Published
  • The Buddha’s Praise of Arahat Sāriputta
  • Selected Poems of Bhikkhu Moneyya
  • The Buddha goes to War
  • Ajahn Suthep: Introduction to Basic Dynamic Meditation
  • BWV 054: Gratefulness to Friends
  • Prasad Bhandarkar: Bansuri Raga Shivranjani
  • Two Chanting Books now in Hard Copy for free Distribution
  • Ven Sumangalo, 01: What is Practical Buddhism?
  • The History and Production of Silk

Recent Posts

  • Dharma Storybooks on Photo Dharma
  • The Exalted Chapters, A Simplified Translation of the Udānavarga
  • Translation of the Sanskrit Udānavarga Published
  • The Lives of Inspiring Chinese Nuns
  • Five New Albums on Photo Dharma
  • 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

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
© 2026 Dharma Records | Powered by Minimalist Blog WordPress Theme