2

I have a function from Turn4JS which generates a book user inteface on a website. The trouble is that the Javascript/JQuery details accept a book size of width and height in absolute pixels, rather than as a percentage or in any other format.

example:

$( document ).ready(function() {
    $("#guestbook").turn({
        width: 500, /* pixels */
        height: 360,
        autoCenter: true
    });
});

The trouble with this is that the website is mobile compatible (of course) and so the size of the book for a small smartphone device should be 300px width maximum, but obviously on larger screens that's going to look very small.

So, I run a media query on the Javascript to set the width/height values based on the browser size. I set this into a function that is fired on document ready as well as on windows resize and window orientation change

Jquery:

function guestbookSize() {
    if ($(window).width() > 1200) {
        $("#guestbookbook").turn({
            width: 700,
            height: 500
        });
    }
    else if ($(window).width() <= 1200 && $(window).width() > 801) {
        $("#guestbookbook").turn({
            width: 500,
            height: 360
        });
    }
    else if ($(window).width() <= 800 && $(window).width() > 360) {
       /* You get the idea */ 
    }
    else if ($(window).width() <= 360) {
        $("#guestbookbook").turn({
            width: 300,
            height: 360
        });
    }
}

$( document ).ready(function() {
    $(document).ready(guestbookSize);
});
$(window).on("orientationchange",guestbookSize);
$(window).resize(guestbookSize);

I'm pretty sure I'm coding this very poorly, but the document load call works, every time.

The window resize call does not work.

So my problem is this:

How can I re-initialise the options for the .turn function within the guestbookSize function without refreshing the whole page?


Notes:

  • I am using JQuery 1.11.3
  • I have read Refire ready event after AJAX reload which seemed similar but no luck.
  • I have explored Turn4JS official documentation which is patchy at best.
  • I did have two choices of either editing the Turn4JS jquery files to accept percentages which looked like a horrific job, or simply making the sizing of the given width/height dependant on client screen size/orientation.

Errors:

Errors encountered on firebug with re-establishement of turn options:

HierarchyRequestError: Node cannot be inserted at the specified point in the hierarchy

..xp("<(?:"+ea+")[\s/>]","i"),ha=/^\s+/,ia=/<(?!area|br|col|embed|hr|img|input|li...

Referencing line 4 of jquery.1.11.3.min.js .

Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132
  • do you get any errors? or it just simply does not resize? – blurfus Feb 27 '16 at 00:11
  • Error added at the bottom, @ochi – Martin Feb 27 '16 at 00:14
  • Can you try `window.addEventListener('resize', function(){}, true);` and see if that makes any difference? – archana Feb 27 '16 at 00:20
  • @archana I have added that listener into the document ready function and that has not changed anything. – Martin Feb 27 '16 at 00:23
  • I added the listeners into the `document.ready` and it seemed to work for me: http://jsfiddle.net/jxxkryuw/1/ - Also, inside your document ready you have another document ready!!?? – blurfus Feb 27 '16 at 00:24
  • @ochi strange, that on my Firefox your fiddle does not resize for me, unless I page refresh, however I like your `initbook` function use, I might nick that, if I may! – Martin Feb 27 '16 at 00:27
  • by all means, use it... :P - @Martin what version of FF are you using? I just tried it on Chrome 48, FF 43, IE9 - they all worked (on Windows) – blurfus Feb 27 '16 at 00:27
  • Oooh, I just noticed, you are using Jquery 1.11.3 - I am using 2.2.1 in the fiddle - are you able to update the jquery version? - **update** nope that's not it, I updated my local fiddle to use same version and it still works – blurfus Feb 27 '16 at 00:33
  • @ochi it's firefox 44 . I will try it on chrome.... – Martin Feb 27 '16 at 00:38
  • the resizing jsfiddle works on Chrome 48. @ochi – Martin Feb 27 '16 at 00:44
  • Updated my FF to 44.0.2 and it also works :$ – blurfus Feb 27 '16 at 00:46

1 Answers1

1

I added event bindings inside the document.ready and removed the duplicates.

Seems to work - tested on on Chrome 48, FF 43, IE9 - they all worked (on Windows)

$(document).ready(function() {
  initBook(300, 400);
  $(window).on("orientationchange", guestbookSize);
  $(window).resize(guestbookSize);
});

function initBook(w, h) {
  $("#flipbook").turn({
    width: w,
    height: h
  });
}

function guestbookSize() {
  if ($(window).width() > 1200) {
    initBook(700, 500);
  } else if ($(window).width() <= 1200 && $(window).width() > 801) {
    initBook(500, 360);
  } else if ($(window).width() <= 800 && $(window).width() > 360) {
    initBook(400, 360);
  } else if ($(window).width() <= 360) {
    initBook(300, 360);
  }
}
/* to see book resize */

#flipbook {
  border: 2px solid green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://www.turnjs.com/lib/turn.min.js"></script>

<h2>
Book
</h2>
<div id="flipbook">
  <div class="hard">Turn.js</div>
  <div class="hard"></div>
  <div>Page 1</div>
  <div>Page 2</div>
  <div>Page 3</div>
  <div>Page 4</div>
  <div class="hard"></div>
  <div class="hard"></div>
</div>
<hr/>
blurfus
  • 13,485
  • 8
  • 55
  • 61
  • Thanks for that. It may be worth a note that I'm using theTurn4 however your fiddle appears to use turn3 . Maybe I should try it with that version rather than 4.... – Martin Feb 27 '16 at 00:42
  • @Martin I updated the fiddle: jquery 1.11.0; turnjs 4 - still works on FF44 and Chrome 48 http://jsfiddle.net/jxxkryuw/4/ – blurfus Feb 27 '16 at 00:51
  • ok thanks for that, how does the real thing look on your browsers? http://test.mhcreations.co.uk/packwaybarn#guestbook . If the page refreshes it always comes up the correct size/scale but with window resize it never does. – Martin Feb 27 '16 at 00:53
  • @Martin I have to go soon but on Chrome, it works as expected - it resizes the book when I resize the browser window (no refreshing needed) – blurfus Feb 27 '16 at 00:54
  • That's awesome. But now I'm confused as to why it's not doing that on my browsers!! Thanks for your help Ochi – Martin Feb 27 '16 at 00:55