Screw – A jQuery Plugin

Screw, a word I re-coined from scroll + view, is a jQuery plugin which loads HTML as a user scrolls the webpage. Screw will help you save bandwidth by only loading content as it is scrolled into view. Screw can also be used to create continuously scrolling, bottomless pages where content is appended to the bottom as the user scrolls.

As of version 1.0.5, Screw supports both vertical AND horizontal scroll detection. Screw can also be called on the BODY selector or any other scrollable object.

Git The Source

Examples – Loading Images
Examples – Loading External And Internal Content
Live Demo – Continuous Vertical Scrolling

Options

Name: loadingHTML
Description: A string consisting of optional HTML which is displayed before the object is loaded. This option is only used if a insertion position class is not used.

Classes

Name: screw
Description: Applies Screw to this object. If you want to load your HTML from an external file use the rel attribute to hold the path to the (local) external file. This uses GET, so you can pass data using the query string. You can alternatively use the rev attribute to hold the HTML you want to load instead of using the rel attribute to load an external file.

Name: screw-before
Description: Inserts the HTML before the object with this class.

Name: screw-after
Description: Inserts the HTML after the object with this class.

Name: screw-append
Description: Appends the HTML to the object with this class.

Name: screw-prepend
Description: Prepends the HTML to the object with this class.

Name: screw-repeat
Description: Repeats screw each time the trigger point is reached. This is for effects like continuous scrolling pages where data is appended to the bottom of the page as the user scrolls.

Name: screw-image
Description: Use the rel attribute to hold a image URL. The image will be loaded into the object with this class. You do not need to use the class screw with this class.

Name: screw-remove
Description: Objects with this class are hidden when they are scrolled beyond the top or right of the viewport.

Name: screw-replace
Description: Replaces the existing content with the new content.

Name: screw-xml
Description: The output data type for objects with this class will be XML. Default is HTML.

Name: screw-json
Description: The output data type for objects with this class will be JSON. Default is HTML.

Name: screw-script
Description: The output data type for objects with this class will be Script. Default is HTML.

Usage

Call screw on the body selector or any other scrollable object and set up your HTML to utilize it.

<script type="text/javascript">
jQuery(function($){
    $("body").screw({
        loadingHTML: '<img alt="Loading" src="loading-image.gif">'
    });   
});
</script>

OR

<script type="text/javascript">
jQuery(function($){
    $("#example-div").screw({
        loadingHTML: '<img alt="Loading" src="loading-image.gif">'
    });   
});
</script>

The rev attribute is used to hold the HTML to load. Be sure to escape the HTML you’re loading. You can use my Postable Code generator to escape your HTML. In the Postable Code generator, select the “None” brush.

<p class="screw" rev="&lt;img src='my-image.jpg' /&gt;"></p>

Or use the rel attribute instead to GET the HTML from a local external file.

<p class="screw" rel="screw-test.php?key1=value&key2=value"></p>

Load the HTML before the object:

<span class="screw screw-before" rev="&lt;img src='my-image.jpg' /&gt;"></span>

Load the HTML after the object:

<p class="screw screw-after" rev="&lt;img src='my-image.jpg' /&gt;"></p>

Append the HTML to the object:

<hr class="screw screw-append" rev="&lt;img src='my-image.jpg' /&gt;" />

Prepend the HTML to the object:

<p class="screw screw-prepend" rev="&lt;img src='my-image.jpg' /&gt;"></p>

Load an image with .screw-image. Use the rel attribute to hold the URL to the image. Can be local or remote.

<span class="screw-image" rel="my-image.jpg">Loading ... </span>

Endless scrolling:

<p class="screw screw-before screw-repeat" rev="&lt;img src='my-image.jpg' /&gt;"></p>

Or

<div class="screw screw-before screw-repeat" rel="next_article.php?key1=value&key2=value"></div>

Control the output data type:

<div class="screw screw-before screw-repeat screw-xml" rel="next_article.php?key1=value&key2=value"></div>

 

Loading Page Fragments

The data-load attribute enables loading of remote webpage-fragments. For example, you can load a specific, remote image or container by adding it’s id or class-name to the data-load attribute as seen in the examples below.

Load a remote object by css class-name:

<div class="screw screw-append" data-load=".my-class" rel="next_article.php?key1=value&key2=value"></div>

Load a remote object by id:

<div class="screw screw-append" data-load="#my-id" rel="next_article.php?key1=value&key2=value"></div>

Using additional selectors:

To aid in selecting specific objects, Screw supports all of the selectors which are supported by jQuery.

<div class="screw screw-append" data-load="#my-id:first" rel="next_article.php?key1=value&key2=value"></div>
<div class="screw screw-append" data-load=".my-class:last" rel="next_article.php?key1=value&key2=value"></div>
<div class="screw screw-append" data-load=".my-class[name='my_input']" rel="next_article.php?key1=value&key2=value"></div>
<div class="screw screw-append" data-load=".my-class:contains('specific text')" rel="next_article.php?key1=value&key2=value"></div>
  • Maxcherry79

    Hello Jason. Just to note (and this may be something I’m missing) but in your docs (both here and on github), you don’t mention anything about the “jloverlay.js”. I wasn’t able to get this script to work without it. Secondly, there are no examples on where/how to implement the “loadingHTML” option.

    Thanks,
    Mike

  • http://jasonlau.biz Jason Lau

    I used jloverlay in the example, but jquery.screw does not depend on it. jloverlay simply masks the images on the example page. If you remove jloverlay.js and the js for it, jquery.screw should work.

    loadingHTML is used like this -
    `jQuery(‘body’).screw({
    loadingHTML: ‘ Loading Please Wait’
    });`

    In my example I added a loading image to the background of the jquery.screw container instead of using the loadingHTML option. I used css instead.

    `.img-container td{
    background-image: url(‘http://jasonlau.biz/home/images/ajax-loader-red.gif’);
    background-repeat: no-repeat;
    background-position: center center;
    }`

  • Bydbest

    H i Jason , for me this is not working. i have similar kind of page where several images are there but all images are loaded at once . in your case it loading one by one. i have saved your plane demo page and tried exactly the same then also not getting it to worked . My php code displays 30 photos but all are coming in one go please tell how to implement it . more documentation is required.

  • http://jasonlau.biz Jason Lau

    I will make some additional examples and documentation.

    The trigger-point is the bottom of the browser window. Any “screw” images which are in-view will load when the page loads. Otherwise, they will not load until they are scrolled to the trigger-point.

    Your webpage needs content which will provide space between the images so all of the images are not higher on the page than the trigger-point when the page loads.

    Theory: When your test page loads, all of your images are higher than the bottom of the window and therefore are all loaded at once. Add enough content or CSS to provide space between each image. For example, If your page is 600px high, add 600px between the images and they will not all load at once. Only those which are in the view-port will load.

  • Bydbest

    Hi , jason i have 1000 images with different link i want to load them one by one, please tell how to do that , i am unable to do that

  • http://jasonlau.biz Jason Lau

    Save the links or images in a database, use server-side scripting to get individual image data from the database, then use jquery.screw to display them on a page. That’s what I’ve done in my example page except I am getting a random image from a database of tens-of-thousands of images.

    Example, get_pics.php?pic_id=999
    or get_pics.php?pic_id={random number}

    With jquery.screw, you have to provide adequate padding between images using other content such as paragraphs so a user has to scroll to view. All of the images in the viewport will be loaded.

  • http://jasonlau.biz Jason Lau

    I am currently working on a new version of this plugin with some better examples. My time is limited, so it will take a few days or longer.

  • Bydbest

    sir i am also using the same thing. in my database several rows are there i am trying to display them. is screw is possible for only images ???

    i tried for images too . i request you please send me raw plain html or php file of your plain demo page including css and js file. my email id is bydbest@gmail.com

  • Webspeeder

    Hello Jason. I am looking to return tabled data in the veiwport, do you have an example of doing this? I’d like to call a program (like PHP) and return the results from it. Or an AJAX call. Is this possible?

  • http://jasonlau.biz Jason Lau

    Yes, this is possible. The following example uses AJAX to GET the output from “tabled_data.php”. The output is appended to the div with the class “screw”. The output can consist of HTML or plain text.

    Important: Be sure to position the div on the webpage BELOW the bottom of the viewport or jquery.screw will load the output immediately when the page loads. Any jquery.screw objects which are positioned in the viewport when the page loads will be loaded immediately.

  • Daan

    Hi,
    Nice work, I’m hoping you could perhaps help me out with expanding the plugin’s functionality. The plugin doesn’t seem to work when scrolling horizontally. Is there a way to modify it so it also checks if the side of the screen is reached?
    Thanks!

  • http://jasonlau.biz Jason Lau

    It’s supposed to already do it. I will look at it. Perhaps there is a bug.

  • http://twitter.com/tenisvermelho tenisvermelho

    Loooove the plugin! Exactly what I need! Spent days searching for something that did something like this, but I have the same problem as Daan. I tested all the versions of the js available here, but couldnt make it work horizontally.
    Thanks!

  • http://jasonlau.biz Jason Lau

    Hi and thanks!
    Due to lack of time, I haven’t released a new version of this plugin yet to support horizontal scrolling. I started working on a new version, but got inundated with large projects before I could complete it. Unfortunately, it only supports vertical scrolling at the present time. I will get back to it as soon as I can.

  • Jason

    Hi, I was finally able to recode this plugin, and it now does support horizontal scroll detection.

  • Jason

    Hi, it now does support horizontal scroll detection as of version 1.0.5.

  • http://webxecutive.com/ Jason

    Hi, version 1.0.5+ supports horizontal scroll detection.

  • Deniz

    is it possible loading an internal with div, span or paragraph without rev?
    in the code of:
    <span class="screw" rev="<div style="background-color:yellow;& id="internal-content">
    i can write only text content. i want to adding there page content. How is it possible?
    And thank you very much for the code.

  • http://websitedev.biz/ Web Developer

    You can use HTML in the rev attribute if you escape it first. You can escape it with my “Postable Code” tool at – http://jsonlau.com/home/postable-code/ .
    Select Brush “None” while using the Postable Code tool.

  • Traceboi

    Hi. I’m loading this into a fancybox lightbox which is ajax. Unfortunately it sees all divs as in the viewport regardless of the fact that they have to be scrolled down to be seen. Is there a way to fix this?

  • http://websitedev.biz/ Web Dev

    You may need to create sufficient margin around the divs so that they are not in the viewport when the page loads. Can you link me to an example?

  • Traceboi

    Unfortunately the places to see it would require a user account on the social site. Essentially, no matter what is done it thinks its within the viewport regardless of padding or margins.

    Is there a way to alter the trigger easily? Perhaps make a button that triggers the event?

  • http://websitedev.biz/ Web Dev

    Sorry, I need an example to work with.

    The plugin basically creates a box which is the same dimensions as the parent. When the page loads, or when the parent is scrolled, the plugin does a hit-test to see if the “screw” objects are touching the box.

    Perhaps you are calling screw on the wrong selector. Be sure the object you are calling screw on is the one that scrolls and not the child of a scrolling container. I hope that makes sense.

    With a little code you could change the plugin to work with a button instead of scrolling.

  • Q Fard

    hi, i want to traverse through a table recordset and show the entries as needed.  I am getting inconsistent  results – i think this is due to the AJAX componenet…  I want to send a variable paramater to show 3 records at a time, where ever I put the +3, I get weird things happening.

    TIA.

  • http://websitedev.biz/ Web Dev

    Hi, can you link me to an example of your script?

  • disqus_iaDY2pPl9n

    Hello
    Thanks for this very good plugin
    Dou you thing is it possible to load only the content of one div present in an external page and not the the rest of this external page?
    Sorry for my bad english and many thanks for your response and exemple

  • http://websitedev.biz/ Web Dev

    Hello, I pushed a new version which allows you to load page-fragments. Go to https://github.com/jasonlau/jQuery-Screw and get the new version.

    Add the data-load attribute to the screw object. Insert the id or class-name for the remote
    object you wish to load.

    Example:
    Loading by ID attribute -
    <p class=”screw” rel=”screw-test.php?key1=value&key2=value” data-load=”#my-div”></p >

    Loading by css class-name -
    <p class=”screw” rel=”screw-test.php?key1=value&key2=value” data-load=”.my-class”></p>

  • disqus_iaDY2pPl9n

    Hello JasonThanks but bug …
    The script have an error on this line:
    if(o.hasClass(‘screw-repeat’){
    I have modified like this:
    if(o.hasClass(‘screw-repeat’)){
    But it dont work vith this

    and it dont work too if I not use data-load as before:

    When you have one moment …
    Thank and sorry again for my english, I french ;-(
    Thierry Subject: [jasonlaubiz] Re: Screw – A jQuery Plugin

  • http://websitedev.biz/ Web Dev

    Hi, Sorry for the bug! I made a new version. Let me know if it works for you. https://github.com/jasonlau/jQuery-Screw

  • http://websitedev.biz/ Web Dev

    I added a example demo (loading-fragments.html, test-content.html) for loading webpage fragments. https://github.com/jasonlau/jQuery-Screw/tree/master/examples

  • disqus_iaDY2pPl9n

    thanks Jason

  • http://websitedev.biz/ Web Dev

    Hi Mayank. Thanks for your comments. I’m not sure now why I coded this plugin the way I did. I will have to revisit it to see. I typically add a random ID to objects to ensure there are no CSS or JavaScript conflicts. I do the same to the ajax requests to prevent caching.

  • dhaval

    yes reallly…great work ..by you..m reall yappreciate with ur effort.Thanks you very much..Friend….