/    Sign up×
Community /Pin to ProfileBookmark

Veiwport size and image folder selection help

Hi
I am trying to select a folder based on veiwport size to display images on a bootsrap slider.
The idea here is to find the veiwport size and populate a variable called folderName then use this to build the img tag.
all works well if I use the folderName without the veiwport conditiuonal statements I would prefare it to have an event listener to monitor browser resize if possible
I am using the code within an asp page
Thank you
Paul M

[CODE]
<script>
//path to img folder based on viewport size

if ($(window).width() < 768) {
// do something for small screens
var folderName = “http://www.myweb.co.uk/bootstrap/images/slider/768/”
}
else if ($(window).width() >= 768 && $(window).width() <= 992) {
// do something for medium screens
var folderName = “http://www.myweb.co.uk/bootstrap/images/slider/768/”
}
else if ($(window).width() > 992 && $(window).width() <= 1200) {
// do something for big screens
var folderName = “http://www.myweb.co.uk/bootstrap/images/slider/1024/”
}
else {
// do something for huge screens
var folderName = “http://www.myweb.co.uk/bootstrap/images/slider/1600/”
}

//images
var imgslide1 = “01.jpg”;
var imgslide2 = “02.jpg”;
var imgslide3 = “03.jpg”;
var imgslide4 = “04.jpg”;
var imgslide5 = “05.jpg”;

//image tag build
document.getElementById(“slide1”).innerHTML = (‘<img src=”‘) + folderName + imgslide1 + (‘”/>’);
document.getElementById(“slide2”).innerHTML = (‘<img src=”‘) + folderName + imgslide2 + (‘”/>’);
document.getElementById(“slide3”).innerHTML = (‘<img src=”‘) + folderName + imgslide3 + (‘”/>’);
document.getElementById(“slide4”).innerHTML = (‘<img src=”‘) + folderName + imgslide4 + (‘”/>’);
document.getElementById(“slide5”).innerHTML = (‘<img src=”‘) + folderName + imgslide5 + (‘”/>’);

</script>

[/CODE]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@paulmilauthorMay 11.2015 — Ok so I found a method. Here it is.

I uploaded modernizr and saved it as modernizr.js and then linked to it in the page header
<script src="bootstrap/js/modernizr.js"></script>[/QUOTE]

I then used the following code. this code includes the bootstrap 3 slider which I removed the img tags as these will be built by the javascript code


<div class="container-fluid"> <!--carousel container -->

<!--carousel -->

<div id="myCarousel" class="carousel slide" data-ride="carousel">

<!-- Indicators -->

<ol class="carousel-indicators">

<li data-target="#myCarousel" data-slide-to="0" class=""></li>

<li class="" data-target="#myCarousel" data-slide-to="1"></li>

<li class="active" data-target="#myCarousel" data-slide-to="2"></li>

</ol>

<div class="carousel-inner" role="listbox">

<div id="slide1" class="item">

</div>

<div id="slide2" class="item">
</div>

<div id="slide3" class="item">
</div>

<div id ="slide4" class="item">
</div>


<div id="slide5" class="item active">
</div>
</div>
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div> <!--/carousel -->

</div> <!-- /carousel container -->

<script>

//select folder based on viewport width

if (Modernizr.mq('(min-width: 350px)')) {

//...

var folderName = "http://www.myweb.com/bootstrap/images/slider/768/";

} else {

//...

}

if (Modernizr.mq('(min-width: 768px)')) {

//...

var folderName = "http://www.myweb.com/bootstrap/images/slider/768/";

} else {

//...

}

if (Modernizr.mq('(min-width: 992px)')) {

//...

var folderName = "http://www.myweb.com/bootstrap/images/slider/1024/";

} else {

//...

}


if (Modernizr.mq('(min-width: 1200px)')) {

//...

var folderName = "http://www.myweb.com/bootstrap/images/slider/1024/";

} else {

//...

}

if (Modernizr.mq('(min-width: 1600px)')) {

//...

var folderName = "http://www.myweb.com/bootstrap/images/slider/1600/";

} else {

//...

}




//images

var imgslide1 = "01.jpg";

var imgslide2 = "02.jpg";

var imgslide3 = "03.jpg";

var imgslide4 = "04.jpg";

var imgslide5 = "05.jpg";


//image tag build

document.getElementById("slide1").innerHTML = ('<img src="') + folderName + imgslide1 + ('"/>');

document.getElementById("slide2").innerHTML = ('<img src="') + folderName + imgslide2 + ('"/>');

document.getElementById("slide3").innerHTML = ('<img src="') + folderName + imgslide3 + ('"/>');

document.getElementById("slide4").innerHTML = ('<img src="') + folderName + imgslide4 + ('"/>');

document.getElementById("slide5").innerHTML = ('<img src="') + folderName + imgslide5 + ('"/>');


</script>[/QUOTE]
×

Success!

Help @paulmil spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...