Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Formats > Workshop

Notices

Reply
 
Thread Tools Search this Thread
Old 09-18-2015, 03:33 PM   #1
RbnJrg
Wizard
RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.
 
Posts: 1,542
Karma: 6613969
Join Date: Mar 2013
Location: Rosario - Santa Fe - Argentina
Device: Kindle 4 NT
Question Help with a javascript

I don't know if what I am looking for may be possible. I need a script (for a epub3 ebook) to apply the property "display: none" or "display: block" with regards to a paragraph, according to the position on the screen of that paragraph. That position will be variable, according to the font size choosen by the user; if the position of the paragraph is in the lower half of the screen (50% or higher), the paragraph must be hidden, otherwise (50% or lower) the paragraph must be visible. Can this be achieved? If so, how?

Many thanks in advance for any info you can give me.

Regards
Rubén
RbnJrg is offline   Reply With Quote
Old 09-20-2015, 04:43 PM   #2
Doitsu
Grand Sorcerer
Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.
 
Doitsu's Avatar
 
Posts: 5,584
Karma: 22735033
Join Date: Dec 2010
Device: Kindle PW2
Out of curiosity, I've looked into this. You could theoretically get the top offset of an element with offsetTop and the available height of the window with window.screen.availHeight and hide the object if the vertical offset is larger than 50% of the available window height.
This method kind of works with a browser and to some extent with ADE4, but I got weird results with other ePub3 apps.
I.e., this method is probably not robust enough for practical use.
I've attached a very simple proof-of-concept test file that you can play with.
(Uncomment //myDiv.parentNode.removeChild(myDiv); to actually delete the div.)
Attached Files
File Type: epub JavaScriptTest.epub (62.1 KB, 357 views)

Last edited by Doitsu; 09-20-2015 at 04:47 PM.
Doitsu is offline   Reply With Quote
Advert
Old 09-20-2015, 09:06 PM   #3
RbnJrg
Wizard
RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.
 
Posts: 1,542
Karma: 6613969
Join Date: Mar 2013
Location: Rosario - Santa Fe - Argentina
Device: Kindle 4 NT
Quote:
Originally Posted by Doitsu View Post
Out of curiosity, I've looked into this. You could theoretically get the top offset of an element with offsetTop and the available height of the window with window.screen.availHeight and hide the object if the vertical offset is larger than 50% of the available window height.
This method kind of works with a browser and to some extent with ADE4, but I got weird results with other ePub3 apps.
I.e., this method is probably not robust enough for practical use.
I've attached a very simple proof-of-concept test file that you can play with.
(Uncomment //myDiv.parentNode.removeChild(myDiv); to actually delete the div.)
Hi Doitsu, thank you very much for your answer. I tell you that I was looking for a solution in the net, and after taking things here and there, I could build the following script:

Code:
function setup() {
var SH = screen.height,
    Element = fleuron.getBoundingClientRect(),
    TopPos = Element.top / SH,
    BottomPos = Element.bottom / SH;

TopPos = TopPos - Math.floor(TopPos);
BottomPos = BottomPos - Math.floor(BottomPos);

alert('Screen Height is ' + SH + ' TopPos is ' + TopPos + ' and Bottom Pos is ' + BottomPos); /* This is for testing purpose only */

if (TopPos >= "0.2" && BottomPos <= "0.8") {
    document.getElementById("fleuron").style.display = "block";
} else {
    document.getElementById("fleuron").style.display = "none";
}
}
window.onload=setup;
Of course, in the .xhtml file I have a div with the id="fleuron" that is the div I want to hide (or not).

I was testing the script on JSFiddle and works fine but in ereaders things are not so good. By the way, for testing, I'm working with epub2 in Calibre and Azardi (they accept epub2 with scripts). The problem with that script is that I don't know how to do in order to fire it when the font size is changed by the user. Since to change the font size is neccesary to use the mouse or the keyboard or touch the screen, I added the following statements after "window.onload=setup;":

document.onkeydown=setup;
document.onkeypress=setup;
document.onkeyup=setup;
document.onclick=setup;
document.onscroll=setup;
document.onchange=setup;
window.ontouchstart=setup;

but things were not perfect When the paragraph was hidden (due to an increase in the font size), I couldn't do it visible again after decreasing the font size (the position of the paragraph was 0, always). So I thought to work with the position of the previous paragraph (a "prefleuron" paragraph); that improved a bit the things but still I can't get the script fired properly.

Below I attach my test epub (an epub2 ebook) so you can understand better what I want to say. Meanwhile, I'm going to study your epub and to see if I can find solutions. Many thanks again for your help!

Rubén
Attached Files
File Type: epub JavaScript.epub (2.9 KB, 325 views)
RbnJrg is offline   Reply With Quote
Old 09-28-2015, 01:31 PM   #4
RbnJrg
Wizard
RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.
 
Posts: 1,542
Karma: 6613969
Join Date: Mar 2013
Location: Rosario - Santa Fe - Argentina
Device: Kindle 4 NT
I could build a script that works perfectly on browsers:

https://jsfiddle.net/RbnJrg/qm8r72mw/

However, it doesn't work in any ereader that supports javascript Could someone here tell me what is wrong with the script?

Many thanks in advance.
RbnJrg is offline   Reply With Quote
Old 09-28-2015, 03:01 PM   #5
Doitsu
Grand Sorcerer
Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.
 
Doitsu's Avatar
 
Posts: 5,584
Karma: 22735033
Join Date: Dec 2010
Device: Kindle PW2
I only have rudimentary JavaScript skills, but I believe that your code probably doesn't work because you forgot to make sure that the page was fully loaded before the script is run. Since I was too lazy to search for a pure JavaScript solution, I simply used jQuery to check the ready state:

Code:
 $(document).ready(function() {
    window.frames["f1"].addEventListener("resize", setup);
  });
I plugged your slightly modified code into the ePub3 wrapper of the file that I attached to the second post and it appears to be working with ADE 4.5 but pretty much nothing else.

BTW, if you want to create a valid epub, you can't use position: absolute; in the CSS. epubcheck will also complain about the name attribute; use id instead. (The attached file will pass epubcheck 4.)

IMHO, you'd be better off looking for a pure CSS solution. Since you're apparently interested in fleurons, have a look at the ::after pseudo selector. Maybe you could manipulate its CSS rule to achieve the desired result.
Attached Files
File Type: epub JavaScriptTest.epub (62.0 KB, 343 views)
Doitsu is offline   Reply With Quote
Advert
Old 09-28-2015, 07:11 PM   #6
RbnJrg
Wizard
RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.
 
Posts: 1,542
Karma: 6613969
Join Date: Mar 2013
Location: Rosario - Santa Fe - Argentina
Device: Kindle 4 NT
Quote:
Originally Posted by Doitsu View Post
I only have rudimentary JavaScript skills, but I believe that your code probably doesn't work because you forgot to make sure that the page was fully loaded before the script is run. Since I was too lazy to search for a pure JavaScript solution, I simply used jQuery to check the ready state:

Code:
 $(document).ready(function() {
    window.frames["f1"].addEventListener("resize", setup);
  });
I plugged your slightly modified code into the ePub3 wrapper of the file that I attached to the second post and it appears to be working with ADE 4.5 but pretty much nothing else.
Thank you very much Doitsu! Not only in ADE works (that alone means a lot) but also in Calibre; it works perfectly under those GUIs. Now I have to figure out why it can't work under Readium and Azardi but it's very good to me that ADE runs the script flawlessly. In my script I had included the statement:

Code:
window.addEventListener("load", setup);
that supposedly it should run the script when the epub was loaded but it seems that is not the same that you code based on jquery (THAT IT DOES WORK!):

Code:
 $(document).ready(function() {
    window.frames["f1"].addEventListener("resize", setup);
  });
Quote:
BTW, if you want to create a valid epub, you can't use position: absolute; in the CSS. epubcheck will also complain about the name attribute; use id instead. (The attached file will pass epubcheck 4.)
Yes Doitsu, you are right and I was aware about the property position in ebooks but as Sigil and ADE accepted it, I include it (do the try enabling "position: absolute" and changing "visibility: normal" in the iframe styles). Regarding the "name" attribute, I don't know what to tell you. Of course, you are right, "name" is deprecated but for <iframe> tags, it seems that "name" is still valid. You included the "id" attribute but Sigil will complain about it (in fact, it doesn't accept the iframe tag neither ). But your epub passes epubcheck 4, so what you wrote is ok!

Quote:
IMHO, you'd be better off looking for a pure CSS solution. Since you're apparently interested in fleurons, have a look at the ::after pseudo selector. Maybe you could manipulate its CSS rule to achieve the desired result.
Yes Rembember this old thread?

https://www.mobileread.com/forums/sho...d.php?t=212300

But I not only interested in fleurons Doitsu; with javascript you can change the layout of any epub; things that are ok with some font-size, they can not be with a new size. I think that the script I'm looking for, it can be very useful for everyone once epub3 becomes popular.

THANK YOU VERY MUCH FOR YOUR HELP.

Rubén
RbnJrg is offline   Reply With Quote
Old 03-30-2017, 08:37 PM   #7
NikaZhenya
Junior Member
NikaZhenya began at the beginning.
 
Posts: 5
Karma: 10
Join Date: Mar 2017
Location: MX
Device: Calibre
Without JQuery

You can change
Code:
$(document).ready(function() {
with
Code:
window.addEventListener('load', function () {
and you dont need JQuery

Example:

Code:
window.addEventListener('load', function () {
    window.frames["f1"].addEventListener("resize", setup);
});
NikaZhenya is offline   Reply With Quote
Old 03-31-2017, 11:38 AM   #8
RbnJrg
Wizard
RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.RbnJrg ought to be getting tired of karma fortunes by now.
 
Posts: 1,542
Karma: 6613969
Join Date: Mar 2013
Location: Rosario - Santa Fe - Argentina
Device: Kindle 4 NT
Quote:
Originally Posted by NikaZhenya View Post
You can change
Code:
$(document).ready(function() {
with
Code:
window.addEventListener('load', function () {
and you dont need JQuery

Example:

Code:
window.addEventListener('load', function () {
    window.frames["f1"].addEventListener("resize", setup);
});
Great tip! Many thanks for it.
RbnJrg is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript in ePub. Vijay ePub 22 12-28-2023 09:43 AM
Glo HTML/javascript ? nicopilami Kobo Developer's Corner 1 09-12-2015 01:33 PM
Javascript and Bookmarklets Polvo Kindle Developer's Corner 0 10-17-2010 05:54 AM
I need Javascript help Nate the great Workshop 4 04-04-2009 12:55 AM
iLiad Javascript? jsc iRex Developer's Corner 1 09-10-2006 07:17 AM


All times are GMT -4. The time now is 06:45 PM.


MobileRead.com is a privately owned, operated and funded community.