2012-12-12 9 views
5

ठीक है, मैं एक खोज खोज रहा हूं और खोज कर रहा हूं, लेकिन मुझे अभी तक सामान्य क्षेत्र में कोई ढूंढना नहीं है। आईएसक्रॉल के साथ बेहद परिचित नहीं है (इस परियोजना के एक पूर्व डेवलपर ने इसे जोड़ा, और अब मैं इसे ले रहा हूं)। मैं यह पता लगाने की कोशिश कर रहा हूं कि iScroll से कहां से शुरू किया जाए।iScroll लाइट ढूंढें जब मैं स्क्रॉल या तत्व के नीचे पहुंच गया है

अभी सभी स्क्रॉलिंग ऊपर और नीचे के अनुसार काम करते हैं। लेकिन मैं विकसित किए गए समग्र ऐप में कुछ कार्यक्षमता जोड़ना चाहता हूं, जैसे पुराने डेटा प्राप्त करना और उस समय सूचीबद्ध नवीनतम डेटा में इसे जोड़ना। एक अनंत स्क्रॉल।

ajax के माध्यम से डेटा में खींच, और इसके साथ काम करने के लिए यह संलग्न करें और iScroll लंबाई ताज़ा करने के लिए नहीं इतना मुद्दा (मैं कम से कम लगता है, अच्छी तरह से पल के लिए) है। मेरा मुद्दा यह है कि उस पल को तब मिल रहा है जब नीचे पहुंच गया है और फ़ंक्शन को फायर कर रहा हूं, मैं डेटा कहूंगा और इसे जोड़ूंगा।

मैं किसी भी उदाहरण कहीं भी तो मैं आशा करता हूं किसी को यहाँ के बाद से यह सवाल बहुत पुराना है मुझे कुछ विचार

उत्तर

11

फेंक कर सकते हैं नहीं मिल रहा। यह समाधान भविष्य के संदर्भों के लिए है।

का उपयोग iScroll v4.2.5:

var scroll = new iScroll('scroll-wrapper'); 
scroll.options.onScrollEnd = function(){ 
    if(Math.abs(this.maxScrollY) - Math.abs(this.y) < 10){ 
     // RUN CODE if scroll is 10px from the bottom. 
    } 
}; 
+0

धन्यवाद। ध्यान दें कि आप कन्स्ट्रक्टर में पारित विकल्पों के हिस्से के रूप में ऑनस्क्रॉल फ़ंक्शन को शामिल कर सकते हैं उदा। नया iScroll ('तत्व', {onScrollEnd: फ़ंक्शन() {...}); – jackocnr

+0

ग्रेट, यही वह है जिसे मैं ढूंढ रहा हूं। – Xinan

+0

इसका उपयोग तब भी किया जा सकता है जब आपको उपयोग करने की आवश्यकता हो, उदाहरण के लिए, iScroll-probs और किसी अन्य 'सुगंध' की विशेषताएं, जैसे ज़ूम, V5 पर। इस उदाहरण में आप बस iScroll-zoom का उपयोग कर सकते हैं और अपने अनंत स्क्रॉल को कॉन्फ़िगर करने के लिए इस ईवेंट का उपयोग कर सकते हैं। – StinkyCat

-1

दिन के साथ करने के बाद, मैं एक चिकनी अनंत स्क्रॉल लागू किया और iscroll4 का उपयोग कर नमूना एप्लिकेशन को ताज़ा करने के खींच रहे हैं। इन दो कार्यक्षमताओं को मैंने बनाए गए एक नमूना ऐप में कार्यान्वित किया था। आशा है कि यह इस मोबाइल ऐप डेवलपर समुदाय के लिए उपयोगी होगा।

पहले यह माना जाता है कि:

  1. आप iscroll4 कार्यों से परिचित (एक आवश्यकता क्योंकि बारे में ज्यादा नहीं कर रहा हूँ एक गुरु भी नहीं) कर रहे हैं।
  2. आप कैसे एक json फ़ाइल या json/php फाइल करने के लिए ajax कॉल करने के लिए पता

मेरे ऐप सारांश:

उपयोगकर्ता मेरे ऐप पर लोड होता है, यह json/php फ़ाइल पर करने के लिए एक ajax कॉल करता है मेरा सर्वर और कुछ मान वापस आ गए हैं।

समस्या प्रकृति: मैं अनंत स्क्रॉलके साथ पुल कार्यों ताज़ा करने के लिए करना चाहते हैं और मुझे नहीं iscroll4 से अलग किसी अन्य ढांचे का उपयोग करना चाहते हैं।

मेरी समाधान:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0"> 
<meta name="apple-mobile-web-app-capable" content="yes"> 
<meta name="apple-mobile-web-app-status-bar-style" content="black"> 
<link href="http://fonts.googleapis.com/css?family=Headland+One%7COpen+Sans:400,300&amp;subset=latin,cyrillic" rel="stylesheet" type="text/css"></link> 
    <link href="css/mystylesheet.css" rel="stylesheet"> 
<style> 

/** 
* 
* Horizontal Scrollbar 
* 
*/ 
.myScrollbarH { 
    position:absolute; 
    z-index:100; 
    height:8px; 
    bottom:1px; 
    left:2px; 
    right:7px 
} 

.myScrollbarH > div { 
    position:absolute; 
    z-index:100; 
    height:100%; 

    /* The following is probably what you want to customize */ 
    background:-webkit-gradient(linear, 0 0, 100% 0, from(#226BF4), to(#226B8F)); 
    background-image:-moz-linear-gradient(top, #226BF4, #226B8F); 
    background-image:-o-linear-gradient(top, #226BF4, #226B8F); 

    border:1px solid #226BF4; 

    -webkit-background-clip:padding-box; 
    -moz-background-clip:padding-box; 
    -o-background-clip:padding-box; 
    background-clip:padding-box; 

    -webkit-box-sizing:border-box; 
    -moz-box-sizing:border-box; 
    -o-box-sizing:border-box; 
    box-sizing:border-box; 

    -webkit-border-radius:4px; 
    -moz-border-radius:4px; 
    -o-border-radius:4px; 
    border-radius:4px; 

    -webkit-box-shadow:inset 1px 1px 0 rgba(255,255,255,0.5); 
    -moz-box-shadow:inset 1px 1px 0 rgba(255,255,255,0.5); 
    -o-box-shadow:inset 1px 1px 0 rgba(255,255,255,0.5); 
    box-shadow:inset 1px 1px 0 rgba(255,255,255,0.5); 
} 


/** 
* 
* Vertical Scrollbar 
* 
*/ 
.myScrollbarV { 
    position:absolute; 
    z-index:100; 
    width:8px;bottom:7px;top:2px;right:1px 
} 

.myScrollbarV > div { 
    position:absolute; 
    z-index:100; 
    width:100%; 

    /* The following is probably what you want to customize */ 
    background:-webkit-gradient(linear, 0 0, 100% 0, from(#226BF4), to(#226B8F)); 
    background-image:-moz-linear-gradient(top, #226BF4, #226B8F); 
    background-image:-o-linear-gradient(top, #226BF4, #226B8F); 

    border:1px solid #226BF4; 

    -webkit-background-clip:padding-box; 
    -moz-background-clip:padding-box; 
    -o-background-clip:padding-box; 
    background-clip:padding-box; 

    -webkit-box-sizing:border-box; 
    -moz-box-sizing:border-box; 
    -o-box-sizing:border-box; 
    box-sizing:border-box; 

    -webkit-border-radius:4px; 
    -moz-border-radius:4px; 
    -o-border-radius:4px; 
    border-radius:4px; 

    -webkit-box-shadow:inset 1px 1px 0 rgba(255,255,255,0.5); 
    -moz-box-shadow:inset 1px 1px 0 rgba(255,255,255,0.5); 
    -o-box-shadow:inset 1px 1px 0 rgba(255,255,255,0.5); 
    box-shadow:inset 1px 1px 0 rgba(255,255,255,0.5); 
} 

/** 
* 
* Pull down styles 
* 
*/ 
#pullDown, #pullUp { 
    background:#fff; 
    height:40px; 
    line-height:40px; 
    padding:5px 10px; 
    border-bottom:1px solid #ccc; 
    font-weight:bold; 
    font-size:14px; 
    color:#888; 
} 
#pullDown .pullDownIcon, #pullUp .pullUpIcon { 
    display:block; float:left; 
    width:40px; height:40px; 
    background:url(images/[email protected]) 0 0 no-repeat; 
    -webkit-background-size:40px 80px; background-size:40px 80px; 
    -webkit-transition-property:-webkit-transform; 
    -webkit-transition-duration:250ms; 
} 
#pullDown .pullDownIcon { 
    -webkit-transform:rotate(0deg) translateZ(0); 
} 
#pullUp .pullUpIcon { 
    -webkit-transform:rotate(-180deg) translateZ(0); 
} 

#pullDown.flip .pullDownIcon { 
    -webkit-transform:rotate(-180deg) translateZ(0); 
} 

#pullUp.flip .pullUpIcon { 
    -webkit-transform:rotate(0deg) translateZ(0); 
} 

#pullDown.loading .pullDownIcon, #pullUp.loading .pullUpIcon { 
    background-position:0 100%; 
    -webkit-transform:rotate(0deg) translateZ(0); 
    -webkit-transition-duration:0ms; 

    -webkit-animation-name:loading; 
    -webkit-animation-duration:2s; 
    -webkit-animation-iteration-count:infinite; 
    -webkit-animation-timing-function:linear; 
} 

@-webkit-keyframes loading { 
    from { -webkit-transform:rotate(0deg) translateZ(0); } 
    to { -webkit-transform:rotate(360deg) translateZ(0); } 
} 

</style> 
</head> 
<body style="overflow: hidden;" onload="do_refresh();"> 
<div class="wrap"> 
<div id="wrapper"> 
     <div id="scroller"> 
     <div id="pullDown"> 
      <span class="pullDownIcon"></span><span class="pullDownLabel">Pull down to refresh...</span> 
     </div> 

    <div class="content"> 
    <input type="hidden" id="last_id">    
      <div id="responsecontainer"></div> 
     </div> 

     <span class="load_more_loading" ></span> 
    </div> 
    </div> 
</div> 


     <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script> 
     <script src="js/iscroll.js"></script> 
<script src="js/pulltorefresh.js"></script> 
<script src="js/infinitescroll.js></script> 

<script src="js/myinitialloadscript.js"></script> 
    <script src="js/mymobileframework.js"></script> <!--independent of this script. eg. whormhole from mosync--> 

    </body> 
     </html> 

अगले myinitialloadscript.js स्क्रिप्ट बनाने के लिए है। यह उस पृष्ठ पर पहली बार सामग्री में लोड होने की अनुमति देता है।

function do_refresh() 
    { 


    var url = "http://localhost/public_html/landing.php?token=908765789897867567687989089786768980&validator=jhjhjjhjhkhj"; //just url params 
    // ------------------------------------------- 

    $.ajax({ 
      type: 'GET', 
      url: url, 
      contentType: "application/json; charset=utf-8", 
      dataType: "jsonp", 
      // Evaluate text as a json expression 
      converters: {"text jsonp": jQuery.parseJSON}, 
      timeout:30000, 
      async: true, 
      jsonp: true, 
      jsonpCallback: "myJsonMethod", 
      beforeSend: function() 
      { 


//add loading image i dint bcos the image or values becomes double in a case of network delay 
      $("#responsecontainer").html("images/loader.gif").show(); 
      }, 
      success: function(data) 
      { 
      ajax.parseJSONP(data); 
      }, 
      error: function(jqXHR, textStatus, errorThrown) 
      { 
      console.log(errorThrown); 
      alert("Could Not Refresh."); 
      $("#responsecontainer").html("");//remove loading image 
      //$("#responsecontainer").load("refresh_h.html"); //load a page with refresh option. 
      } 
     }); 
    $.ajaxSetup({ cache: false }); //fetch data from db not cache content 
    } 


//if data is fectched successfully then 

    var ajax = { 

     parseJSONP:function(data){ 



      $.each(data, function(i, row) { 
      if (i==0){ 
      $("#responsecontainer").html(''); 
      } 

      var ul = '<li>'+row.myjsondatakey+'</li>'; //could be <div></div> This only displays your values from db in a styled manner 
      $(ul).appendTo('#responsecontainer');// append the value to the responsecontainer 

//Now watch closely. remember it was assumed you have a json file or json/php file, and in 
//most cases you get your json data format from PHP file. so we assume its from a php file. 
//also note that php calls to db can have a LIMIT. so in this case my PHP file data LIMIT 
//was set to 5. because of infinite scrolling, i need to know the id of the last element 
//from my returned data. so i write the following. note that my row.id_comments which is my 
//value i want to get was written into an input whose visibility is hidden. this is to 
//enable me over write its value when the last id changes. 
      if (i==4) 
      { 
      //set last id value 
      $('#last_id').val(row.id_comments); 
      } 

    }); 
    } 
    } 

अगले pulltorefresh.js ज्यादा इस बारे में कहा जा woun't क्योंकि iscroll4 में उदाहरण स्पष्ट है लागू करने के लिए है।

var myScroll, 
    pullDownEl, pullDownOffset, 
    pullUpEl, pullUpOffset, 
    generatedCount = 0; 

function pullDownAction() { 
     var el; 
     el = document.getElementById('responsecontainer'); 

var url = "http://localhost/public_html/landing.php?token=78654567897654356789976546789&valid=jhjhjjhjhkhj"; 

     $.ajax({ 
     type: 'GET', 
     url: url, 
     contentType: "application/json; charset=utf-8", 
     dataType: "jsonp", 
     // Evaluate text as a json expression 
     converters: {"text jsonp": jQuery.parseJSON}, 
     timeout:30000, 
     async: true, 
     jsonp: true, 
     jsonpCallback: "myJsonMethod", 
     error: function(){ 
     myScroll.refresh(); //do nofin 
     }, 
     success: function(data){ 
     //console.dir('success'); 
     ajax.parseJSONP(data); 
     } 
     }); 
$.ajaxSetup({ cache: false }); //fetch data from db not cache content 


var ajax = { 

    parseJSONP:function(data){ 
     $.each(data, function(i, row) { 

     if (i==0){ 
     $("#responsecontainer").html(''); 
     } 

     var ul = '<li>'+row.myjsondatakey+'</li>'; 
     $(ul).appendTo(el); 


if (i==4) 
      { 
      //set last id value 
      $('#last_id').val(row.id_comments); 
      } 
     myScroll.refresh(); // Remember to refresh when contents are loaded (ie: on ajax completion) 

}); 
} 
} 
} 



    function loaded() { 
     pullDownEl = document.getElementById('pullDown'); 
     pullDownOffset = pullDownEl.offsetHeight; 

     myScroll = new iScroll('wrapper', { 
      scrollbarClass: 'myScrollbar', 
      useTransition: true, 
      topOffset: pullDownOffset, 
      onRefresh: function() { 
       if (pullDownEl.className.match('loading')) { 
        pullDownEl.className = ''; 
        pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Pull down to refresh...'; 
       } 
      }, 
      onScrollMove: function() { 
       if (this.y > 5 && !pullDownEl.className.match('flip')) { 
        pullDownEl.className = 'flip'; 
        pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Release to refresh...'; 
        this.minScrollY = 0; 
       } else if (this.y < 5 && pullDownEl.className.match('flip')) { 
        pullDownEl.className = ''; 
        pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Pull down to refresh...'; 
        this.minScrollY = -pullDownOffset; 
       } 
      }, 

//here is where infinite scroll comes in 
       onScrollEnd: function() { 
       //infinite scroll started 
       if(Math.abs(this.maxScrollY) - Math.abs(this.y) < 10) 
       { 
       // Do infinite if scroll is 10px from the bottom. 
       doInfinite_scroll(); 
       } 

       //infinite scroll ended 

       if (pullDownEl.className.match('flip')) { 
        pullDownEl.className = 'loading'; 
        pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Loading...';     
        pullDownAction(); // Execute custom function (ajax call?) 
       } 
      } 
     }); 

     setTimeout(function() { document.getElementById('wrapper').style.left = '0'; }, 800); 
    } 


    document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false); 

    document.addEventListener('DOMContentLoaded', function() { setTimeout(loaded, 200); }, false); 

और पिछले नहीं बल्कि कम से कम मुख्य infinitescroll.js याद करते हैं कि हम पहले pulltorefresh.js में इस समारोह में जहाँ हम कहा कि अगर स्क्रॉल पट्टी अंत inifite स्क्रॉल शुरू हो जाना चाहिए करने के लिए 10px है के लिए एक कॉल रखा था।फिर हमारे पास इसे गोली मारने के लिए शॉट के रूप में है।

function doInfinite_scroll() 
{ 
     var last_id = $('#last_id').val(); 
     var searching = false; 
     if (!searching) { // only initiate a new ajax request if this variable is false this is to avoid data duplication. 
     var el; 
     el = document.getElementById('responsecontainer'); 
     var url = "http://localhost/public_html/landing_more.php?last_id="+last_id+"&token=9876543456789765432456789876543&valid=jhjhjjhjhkhj"; 
     // ------------------------------------------- 

     $.ajax({ 
     type: 'GET', 
     url: url, 
     contentType: "application/json; charset=utf-8", 
     dataType: "jsonp", 
     // Evaluate text as a json expression 
     converters: {"text jsonp": jQuery.parseJSON}, 
     timeout:30000, 
     async: true, 
     jsonp: true, 
     jsonpCallback: "myJsonMethod", 
     error: function(){ 
     myScroll.refresh(); //do nofin 
     }, 
     beforeSend: function() 
     { 
     //add loading image i dint bcos the image or values becomes double in a case of network delay 
     searching = true; // set variable to true 
     }, 
     success: function(data){ 
     searching = false; // set variable to false 
     //console.dir('success'); 
     ajax.parseJSONP(data); 

     } 
     }); 
     $.ajaxSetup({ cache: false });//fetch data from db not cache content 
     var ajax = { 

     parseJSONP:function(data){ 
     $.each(data, function(i, row) { 


     var ul = '<ul>'+row.comments+'</ul>'; 
     if (i==1) //bcos its php data limit was set to 2 so its always N-1 i.e yourlimit - 1 
     { 
     //resset lastdeed id value 
     $('#lastdeed_id').val(row.id_comments); //update the value in the hidden input field to enable the next set of data load properly 
     } 
     $(ul).appendTo(el); 
     myScroll.refresh();  // Remember to refresh when contents are loaded (ie: on ajax completion) 

}); 
} 
} 
} 
} 

तो अगर आप यहां खोए गए php फ़ाइल को देखना चाहते हैं तो आप खो गए हैं। इस फ़ाइल में landing.php

<?php 
// Prevent caching. 
header('Cache-Control: no-cache, must-revalidate'); 
header('Expires: Mon, 01 Jan 1996 00:00:00 GMT'); 

if ((isset($_GET['token'])) && (isset($_GET['valid']))) { 

@require_once('connect/connectionfile.php'); 

$token=htmlspecialchars($_GET['token'],ENT_QUOTES); 
$token= mysql_real_escape_string($token); 
$valid=htmlspecialchars($_GET['valid'],ENT_QUOTES); 
$valid= mysql_real_escape_string($valid); 


//now validating the token 
$sql="SELECT * FROM bla WHERE blabla='$token'"; 
$result=mysql_query($sql); 

//if token exists 
if(mysql_num_rows($result)) 
{ 
header('Content-type: application/json'); 
//valid token 
$fakevalue = mysql_fetch_assoc($result); 
$uid = $fakevalue['id']; 
$results = array(); 
$query = "SELECT * FROM bla bal WHERE bla bla bla DESC LIMIT 0, 5"; 
$rsult = mysql_query($query); 
while($value = mysql_fetch_assoc($rsult, MYSQL_ASSOC)) 
$results[] = $value; 
{ 
echo "myJsonMethod".'('.json_encode($results).')'; //ECHO RESULTS IN JSONP FORMAT 

} 

} 
else 
{ 
//Invalid token 
header('Content-type: application/json'); 
echo "myJsonMethod({\"token\":".utf8_encode(json_encode('failed'))."})"; 
} 
} 
else { 
    header('Content-type: application/json'); 
    echo "myJsonMethod({\"token\":".utf8_encode(json_encode('Invalid token check parameters'))."})"; 
    } 
?> 

यह अगले फ़ाइल अनंत स्क्रॉल के लिए है और यह landing_more.php कहा जाता है

<?php 
// Prevent caching. 
header('Cache-Control: no-cache, must-revalidate'); 
header('Expires: Mon, 01 Jan 1996 00:00:00 GMT'); 

if ((isset($_GET['token'])) && (isset($_GET['validator']))) { 

@require_once('connect/connectionfile.php'); 


$token=htmlspecialchars($_GET['token'],ENT_QUOTES); 
$token= mysql_real_escape_string($token); 
$validator=htmlspecialchars($_GET['validator'],ENT_QUOTES); 
$validator= mysql_real_escape_string($validator); 
$last_id=htmlspecialchars($_GET['last_id'],ENT_QUOTES); 
$last_id=mysql_real_escape_string($last_id); 


//now validating the token 
$sql="SELECT * FROM bla WHERE blabla='$token'"; 
$result=mysql_query($sql); 

//if token exists 
if(mysql_num_rows($result)) 
{ 
header('Content-type: application/json'); 
//valid token 
$fakevalue = mysql_fetch_assoc($result); 
$uid = $fakevalue['id']; 
$results = array(); 
$query = "SELECT * FROM bla bla WHERE bla && bla bla && a.id_comments < '$last_id' ORDER BY a.id_comments DESC LIMIT 0, 2"; 
$rsult = mysql_query($query); 
while($value = mysql_fetch_assoc($rsult, MYSQL_ASSOC)) 
$results[] = $value; 
{ 
echo "myJsonMethod".'('.json_encode($results).')'; //ECHO RESULTS IN JSONP FORMAT 

} 

} 
else 
{ 
//Invalid token 
header('Content-type: application/json'); 
echo "myJsonMethod({\"token\":".utf8_encode(json_encode('failed'))."})"; 
} 
} 
else { 
    header('Content-type: application/json'); 
    echo "myJsonMethod({\"token\":".utf8_encode(json_encode('Invalid token check parameters'))."})"; 
    } 
?> 
संबंधित मुद्दे