chop 10 Posted July 22, 2014 Report Share Posted July 22, 2014 This, I suppose, is as much an HTML question as a js or jQuery question so I'm not sure if this is the proper forum. But, here goes.... I'm trying to use a jQuery image rotater function in my "responsive" html design. My Bootstrap 3 framework let's me display html layout differently depending on the screen size. THE PROBLEM is that I want to use the same jQuery function (which uses the id #fader) to format the page differently according to the screen size. However, referencing the same #id more than once on a page is not allowed. So, I end up making multiple versions of the function "fader" and just change the id name. That results in something that works but uses lots of code. I'm looking for suggestions on how to handle this by using the function only once( I already tried changing "#fader" to ".fader" so to make it a class instead of an id but it did not work (I'm not a great js programmer however). Please refer to this script sample below.Here's the HTML code that accesses the function according to screen size. Here, I have simply made two copies of the function with a different id name for each (fader.txt and fader2.txt): <!--IMAGE FADER for DESKTOP screens--> <div class="visible-lg"> <script src="assets/js/fader.txt"></script> <div style="position:absolute;top:100px; left:650px"> <div id="fader" class="visible-lg"> <img src="images/standInImage.jpg" width="500"> <img src="images/standin2.jpg" width="500"> <img src="images/standin3.jpg" width="500"> <img src="images/standin4.jpg" width="500"> </div> </div> </div> <!--IMAGE FADER for TABLET screens--> <div class="visible-md visible-sm"> <script src="assets/js/fader2.txt"></script> <div style="position:absolute;top:150px; left:450px"> <div id="fader2"> <img src="images/standInImage.jpg" width="350"> <img src="images/standin2.jpg" width="350"> <img src="images/standin3.jpg" width="350"> <img src="images/standin4.jpg" width="350"> </div> </div> </div> Here's the jQuery code itself using "#fader" $(function() { $("#fader img:not(:first)").hide(); $("#fader img").css("position", "absolute"); $("#fader img").css("top", "0px"); $("#fader img").css("left", "50%"); $("#fader img").each(function() { var img = $(this); $("<img>").attr("src", $(this).attr("src")).load(function() { img.css("margin-left", -this.width / 2 + "px"); }); }); var pause = false; function fadeNext() { $("#fader img").first().fadeOut().appendTo($("#fader")); $("#fader img").first().fadeIn(); } function fadePrev() { $("#fader img").first().fadeOut(); $("#fader img").last().prependTo($("#fader")).fadeIn(); } $("#fader, #next").click(function() { fadeNext(); }); $("#prev").click(function() { fadePrev(); }); /* $("#fader, .button").hover(function() { pause = true; },function() { pause = false; });*/ function doRotate() { if(!pause) { fadeNext(); } } var rotate = setInterval(doRotate, 5000); }); I will need a total of 4 layouts for 4 different screen sizes for a single html page. I hope I can do this without having to load 4 copies of the same script but with different id names. Thank you. I hope I have stated this problem clearly. Quote Link to post Share on other sites
Antonio Conte 426 Posted July 22, 2014 Report Share Posted July 22, 2014 Refactor out the functionality you need in a new function, and take the HTML element to change as an argument: function something(element) { var element = $(element); // do stuff } something("#fader"); something("#other"); Quote Link to post Share on other sites
HartleySan 826 Posted August 3, 2014 Report Share Posted August 3, 2014 chop, which jQuery carousel plug-in/method are you using? Please provide a link. Thank you. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.