Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How to write percentage in Javascript/ Jquery

Featured Replies

Hi guys,

 

I have this script for a simple slideshow below and I need the box of the slideshow to be in percentage rather then pixels. I highlighted the part where I think it should go but how do i go about in doing this>?

 

Thank you so much

 

 

<script type="text/javascript">

$(document).ready(function() {

 

var currentPosition = 0;

var slideWidth = 600;

var slides = $('.slide');

var numberOfSlides = slides.length;

var slideShowInterval;

var speed = 9000;

 

 

slideShowInterval = setInterval(changePosition, speed);

 

slides.wrapAll('<div id="slidesHolder"></div>')

 

slides.css({ 'float' : 'left' });

 

$('#slidesHolder').css('width', slideWidth * numberOfSlides);

 

 

function changePosition() {

if(currentPosition == numberOfSlides - 1) {

currentPosition = 0;

} else {

currentPosition++;

}

moveSlide();

}

 

 

function moveSlide() {

$('#slidesHolder')

.animate({'marginLeft' : slideWidth*(-currentPosition)});

}

 

});

</script><!-- Rotator script stops -->

I think you should get the width of the screen with screen.availWidth.

 

Than you just have to divide the amount of pixels with the available width , and multiply it by 100.

Lets say you want to have the precentage of 600 pixels:

 

precentage=600/screen.availWidth*100;

 

Now you can use precentage in your jQuery code.

Edited by Leonvv

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.