Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble with the CSS to change start position on a slider
    primarykey
    data
    text
    <p>I'm very new to CSS and Javascript, and as a sort of project have been working on a slider that moves in two directions, both horizontally and vertically. I've used <a href="http://css-tricks.com/slider-with-sliding-backgrounds/" rel="nofollow">this guide</a> as a model, and have something that is mostly serviceable. </p> <p>However, I'd like for the slider to 'begin' not at the standard point of origin (that is, the left-most and upper-most slide) but at a different, customizable point--for instance, the horizontal and vertical centermost of the available slides--and nothing I try helps me to do this. I've played around with margins, positions and padding, but everything only messes the slider up. Does anyone have an idea for how I can change the slide that is showing on pageload?</p> <p>Here's the CSS that I have so far:</p> <pre><code> .testprojbody { background-color: black; padding: 0; overflow: hidden; } .slider-holder { width: 98%; height: 665px; border: 2px black solid; background-color: white; float: center; margin-left: 9px; } .slider { width: 987px; height: 610px; overflow: hidden; float:center; margin-top: 25px; border: 2px black solid; margin-left: 35px; } .holder { width: 200%; height: 200%; position: relative; } .slide { float: left; width: 987px; height: 610px; position: relative; } .slider-navright { text-align: center; margin: 310px 0 0 1030px; position: absolute; } .slider-navright a { width: 0; height: 0; display: inline-block; overflow: hidden; text-indent: -9999px; border-top: 40px solid transparent; border-bottom: 40px solid transparent; border-left: 20px solid #999; } .slider-navleft { text-align: center; margin: 310px 0 0 12px; position: absolute; } .slider-navleft a { width: 0; height: 0; display: inline-block; overflow: hidden; text-indent: -9999px; border-top: 40px solid transparent; border-bottom: 40px solid transparent; border-right: 20px solid #999; } .slider-navtop { text-align: center; margin: 2px 0 0 501px; position: absolute; } .slider-navtop a { width: 0; height: 0; display: inline-block; overflow: hidden; text-indent: -9999px; border-left: 40px solid transparent; border-right: 40px solid transparent; border-bottom: 20px solid #999; } .slider-navbot { text-align: center; margin: 642px 0 0 501px; position: absolute; } .slider-navbot a { width: 0; height: 0; display: inline-block; overflow: hidden; text-indent: -9999px; border-left: 40px solid transparent; border-right: 40px solid transparent; border-top: 20px solid #999; } </code></pre> <p>Here's the Javascript, which allows for nav &amp; animation and so on:</p> <pre><code>&lt;script type="text/javascript"&gt; var positionH = 0 var positionV = 0 $(document).ready(function(){ var slider = { el: { slider: $("#slider"), allSlides: $(".slide"), sliderNavRight: $(".slider-navright"), sliderNavLeft: $(".slider-navleft"), sliderNavTop: $(".slider-navtop"), sliderNavBot: $(".slider-navbot"), }, timing: 400, slideWidth: 987, slideHeight: 610, // In this simple example, might just move the // binding here to the init function init: function() { this.bindUIEvents(); }, bindUIEvents: function() { // nav code this.el.sliderNavRight.on("click", "a", function(event) { slider.handleNavRightClick(event, this); }); this.el.sliderNavLeft.on("click", "a", function(event) { slider.handleNavLeftClick(event, this); }); this.el.sliderNavTop.on("click", "a", function(event) { slider.handleNavTopClick(event, this); }); this.el.sliderNavBot.on("click", "a", function(event) { slider.handleNavBotClick(event, this); }); }, handleNavRightClick: function(event, el) { positionH+=1; event.preventDefault(); this.el.slider.animate({ scrollLeft: this.slideWidth * positionH }, this.timing); }, handleNavLeftClick: function(event, el) { positionH-=1; event.preventDefault(); this.el.slider.animate({ scrollLeft: this.slideWidth * positionH }, this.timing); }, handleNavTopClick: function(event, el) { event.preventDefault(); positionV--; this.el.slider.animate({ scrollTop: this.slideHeight * positionV }, this.timing); }, handleNavBotClick: function(event, el) { event.preventDefault(); positionV++; this.el.slider.animate({ scrollTop: this.slideHeight * positionV }, this.timing); }, }; slider.init();}); &lt;/script&gt; &lt;script type="text/javascript"&gt; //arrow functions $(document.documentElement).keydown(function(event){ if (event.keyCode == 39){ //go right event.preventDefault(); $('.slider-navright a') .click(); } else if (event.keyCode == 37){ //go left event.preventDefault(); $('.slider-navleft a') .click(); } else if (event.keyCode == 38){ //go up event.preventDefault(); $('.slider-navtop a') .click(); } else if (event.keyCode == 40){ //go down event.preventDefault(); $('.slider-navbot a') .click(); } }); // makes slider unselectable AND makes arrow nav work better $(".slider").disableSelection(); &lt;/script&gt; </code></pre> <p>and here's the relevant HTML:</p> <pre><code>&lt;body class="testprojbody"&gt; &lt;div class="slider-holder"&gt; &lt;div class="slider" id="slider"&gt; &lt;div class="holder"&gt; &lt;div class="slide" id="slide-x0y0"&gt;&lt;/div&gt; &lt;div class="slide" id="slide-x1y0"&gt;&lt;/div&gt; &lt;div class="slide" id="slide-x0yA"&gt;&lt;/div&gt; &lt;div class="slide" id="slide-x0y0"&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;nav class="slider-navright"&gt; &lt;a href="#"&gt;Move Right&lt;/a&gt; &lt;/nav&gt; &lt;nav class="slider-navleft"&gt; &lt;a href="#"&gt;Move Left&lt;/a&gt; &lt;/nav&gt; &lt;nav class="slider-navtop"&gt; &lt;a href="#"&gt;Move Up&lt;/a&gt; &lt;/nav&gt; &lt;nav class="slider-navbot"&gt; &lt;a href="#"&gt;Move Down&lt;/a&gt; &lt;/nav&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Hope this is comprehensible, as I said, I'm very new (only picked up javascript about two weeks ago, and html maybe a month and a half ago), so I'm sure this is very sloppy, roundabout code. Still, if anyone could help, it would be much appreciated!</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload