Single page, carousel-style, slide website:Navigate to previous slide, on browser back button, without defaulting to home slide?

Desmond Mond

I am making a website that uses div/slides to hold content. They are positioned inside a container with overflow:hidden. A navigation bar uses document.getElementById('box').style.left='0px' to alter the div positions to bring required div into view. The left CSS property is changed 'onclick' to bring the targeted div into view and push the others out of container viewing area. Transitions animations are handled by CSS.

Here a link to the fiddle example. http://jsfiddle.net/HyHmF/1/ . HTML Code is as follows,

<a class="boxes" href="#" onclick="document.getElementById('box1').style.left='0px';
                                   document.getElementById('box2').style.left='200px';
                                   document.getElementById('box3').style.left='400px';">Box 1</a>

<a class="boxes" href="#" onclick="document.getElementById('box1').style.left='-200px';
                      document.getElementById('box2').style.left='0px';
                      document.getElementById('box3').style.left='200px'";>Box 2</a>

<a class="boxes" href="#" onclick="document.getElementById('box1').style.left='-400px';
                      document.getElementById('box2').style.left='-200px';
                      document.getElementById('box3').style.left='0px'">Box 3</a>
<div id="boxcontainer">
<div id="box1">Box 1</div>
<div id="box2">Box 2</div>
<div id="box3">Box 3</div>
</div>  

My issue is this. How can I make the browser back button to change the CSS to bring the previous displayed div into view. That is, without reverting back to the 'home' div.

I am reasonably familiar with javascript and jquery. I know the answer lies in JQuery BBQ pushstates and hashtags. Unfortunately I haven't been able to implement these into my site @ http://www.sumoto.com.au. The fiddle is basically this site, scaled down, for ease of explaining my dilemma.

Essentially, how can I save a 'snapshot', of the current css view to a hashtag url, and call this 'save' through the browser back/forward navigation?

Desmond Mond

I solved my problem!

I removed all the cluttered onclick lines from the a elements. And added id's for each one. The actual hashtag/browser back button functionality is provided by hashchange by Ben Alman, and of course, JQuery.

The revised HTML is:

<a class="boxes" id="link1" href="#box_1">Box 1</a>
<a class="boxes" href="#box_2">Box 2</a>
<a class="boxes" href="#box_3">Box 3</a>                     
<div id="boxcontainer">
<div id="box1">Box 1</div>
<div id="box2">Box 2</div>
<div id="box3">Box 3</div>
</div>

The Javascript

$(document).ready(function() {
    $(window).hashchange( function(){
    var hash = location.hash;
    var lc1 = $("#link1")
    if (lc1.attr('href') === hash) {document.getElementById('box1').style.left='200px';}
    if (lc1.attr('href') !== hash) {document.getElementById('box1').style.left='0px';}
    });
    $(window).hashchange();
});

The a links are targeted by the script and assigned a var; "lc1". An if statement checks if the hash of lc1 (the link) is equal to the browser url. If it is then the css manipulation is performed and the sliding movement occurs. If it isn't (for example when the user clicks back) the boxes return to their original (or previous) conformation.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

finding element in the active slide of Bootstrap carousel

분류에서Dev

Is it possible to navigate through routes like in TabBar(with left slide) but without using TabBar but instead using buttons

분류에서Dev

Slide out element to right, then back in from left

분류에서Dev

Slide-to-the-right submenu after clicking a button

분류에서Dev

extend images across slide active slide

분류에서Dev

Jquery Slide up and Slide down toggle

분류에서Dev

navigation slide in issue

분류에서Dev

Stop slide when ended

분류에서Dev

Slide down effect on mobile

분류에서Dev

Event triggering on slide appearance

분류에서Dev

ListView swipe/slide animation

분류에서Dev

What is this slide bar in gnome?

분류에서Dev

Radio button clicked to slide & show hidden div & pass a value

분류에서Dev

jQuery Slide round button(pre-next) custom

분류에서Dev

full page js slide next section over current

분류에서Dev

full page js slide next section over current

분류에서Dev

ActiveX control inside PowerPoint slide

분류에서Dev

Finger controlled view slide animation

분류에서Dev

multiple jquery slide toggle not working

분류에서Dev

JQuery Slide Down 위치

분류에서Dev

How to stop looping slide in jQuery

분류에서Dev

How to slide these divs continuously in loop

분류에서Dev

Gnome, Lock Screen Slide show

분류에서Dev

Bootstrap carousel slide.bs.carousel 또는 slid.bs.carousel 이벤트가 Angular로 실행되지 않음

분류에서Dev

PowerPoint: reuse a slide multiple times in a presentation

분류에서Dev

jquery cycle pager #nav disappears on second slide

분류에서Dev

Add text to PowerPoint slide during a presentation

분류에서Dev

Slide-swipe menu in front of view

분류에서Dev

Jquery slide hide-show div with an image

Related 관련 기사

뜨겁다태그

보관