2009-12-20 103 views
1

我試圖在內容滑塊與此類似網站影響工作:MonitterjQuery的內容滑塊

不像這個網站,我不希望包括任何實時Twitter更新。我只是希望div中的內容以類似的方式加載。

我已經看過具有垂直內容滑塊的各種jquery插件,但沒有一個似乎具有預期的效果。

最後,我不是在尋找完整的代碼,只是一些小的幫助,以便我可以開始朝着正確的方向工作。

編輯:在連續div之間需要有一段時間延遲,以便用戶可以瀏覽每個div中的內容。

編輯:我想我應該澄清一些代碼示例這次。我不geeting對這些早期道歉:在jsondata.php文件

<?php 
    $json = '{ 
     "userdata": [ 
      { 
       "first":"James", 
       "last":"Watt", 
       "email":"[email protected]", 
       "city":"xyz" 
      }, 
      { 
       "first":"Isaac", 
       "last":"Newton", 
       "email":"[email protected]", 
       "city":"xyz" 
      }, 
      { 
       "first":"Albert", 
       "last":"Einstein", 
       "email":"[email protected]", 
       "city":"xyz" 
      } 
         ] 
        }'; 
       echo $json; 
?> 

繼 這裏的樣本數據是的index.html文件

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
     <title>Untitled Document</title> 
     <link rel="stylesheet" type="text/css" href="style2.css" /> 
     <script type="text/javascript" src="lib/jquery/jquery-1.3.2.js"></script> 
     <script type="text/javascript"> 
      $(document).ready(function(){ 
       $("#loaduserdata").click(function(){ 
       $("#info").empty(); 
      $.getJSON(
      "jsondata.php", 
      function(data){ 
       $.each(data.userdata, function(i,user){ 
        var html = '<div class="entry">'; 
         html += "<p>"+user.first+"</p>"; 
         html += "<p>"+user.last+"</p>"; 
         html += "<p>"+user.email+"</p>"; 
         html += "<p>"+user.city+"</p>"; 
         html += '</div>'; 
        $(html).appendTo('#info'); 
       }); 
      }); 
    }); 

}); 
     </script> 
    </head> 
    <body> 
     <a href="#" id="loaduserdata">User Data</a> 
     <div id="info"></div> 
    </body> 
</html> 

現在,點擊用戶數據將通過類輸入快速填充div中的所有數據。我正在尋找的是以類似於monitter網站的風格填充數據,也就是說,數據應該在一段時間內緩慢填充。 這是一些示例代碼。如果我走錯了方向,請糾正我。

+0

你看看jQuery UI的? – kiamlaluno 2009-12-20 07:20:52

+0

你好,我已經看過它了。我仍在使用它,但目前尚無法取得預期的結果。 – 2009-12-20 08:02:55

回答

2

這就是我要做的(demo here)...實際的代碼包含在addRow函數中。我在內容中添加了一個簡單的「插入日期」,您可以使用ajax來檢索任何內容。

其餘的代碼是爲了演示的目的,主要是因爲我不喜歡div的屏幕出現,所以我添加了每列div的最大數量。

CSS

#wrapper { position: relative; left: 0; top: 0; width: 600px; margin: 0 auto; text-align: center; } 
.column { position: relative; float: left; padding: 0; width: 50%; } 
.column div { background: #eee; border: #333 1px solid; margin: 5px; padding: 5px; } 
.column .top { background: #ccc; } 
.newrow { display: none; } 

HTML

<input type="button" rel="col1" style="margin: 5px; float: right;" value="Add row to right" /> 
<input type="button" rel="col0" style="margin: 5px; float: right;" value="Add row to left" /> 
<input type="button" rel="both" style="margin: 5px; float: right;" value="Add row to both" /> 

<div id="wrapper"> 
<div id="col0" class="column"><div class="top">Content added below</div></div> 
<div id="col1" class="column"><div class="top">Content added below</div></div> 
</div> 

腳本

$(document).ready(function(){ 
var maxRows = 10; 
$(':button').click(function(){ 
    var el = $(this).attr('rel'); 
    if (el=="both") { el="col1"; addRow("col0"); } 
    addRow(el); 
    // remove extra rows 
    $('#wrapper').find('.column').each(function(){ 
    $(this).find('.row:gt(' + maxRows + ')').remove(); 
    }) 
}); 
}) 

function addRow(el){ 
// get whatever contents here 
var content = (new Date).toString(); 
// add new row 
$('#'+el).find('.top').after('<div class="newrow row">' + content + '</div>') 
$('.newrow').fadeIn('slow',function(){ $(this).removeClass('newrow') }); 
} 

更新:好吧,我看你也沒問的代碼,但是我在這一點一起......它應該更符合你想要的東西。

假設這JSON結構:

({ 
"news": [ 
    { "id" : "0010", "date" : "Sun Dec 20 2009 12:10:00 GMT-0500 (Eastern Standard Time)", "title" : "News item 10" }, 
    { "id" : "0009", "date" : "Sun Dec 20 2009 12:09:00 GMT-0500 (Eastern Standard Time)", "title" : "News item 9" }, 
    { "id" : "0008", "date" : "Sun Dec 20 2009 12:08:00 GMT-0500 (Eastern Standard Time)", "title" : "News item 8" }, 
    { "id" : "0007", "date" : "Sun Dec 20 2009 12:07:00 GMT-0500 (Eastern Standard Time)", "title" : "News item 7" }, 
    { "id" : "0006", "date" : "Sun Dec 20 2009 12:06:00 GMT-0500 (Eastern Standard Time)", "title" : "News item 6" }, 
    { "id" : "0005", "date" : "Sun Dec 20 2009 12:05:00 GMT-0500 (Eastern Standard Time)", "title" : "News item 5" }, 
    { "id" : "0004", "date" : "Sun Dec 20 2009 12:04:00 GMT-0500 (Eastern Standard Time)", "title" : "News item 4" }, 
    { "id" : "0003", "date" : "Sun Dec 20 2009 12:03:00 GMT-0500 (Eastern Standard Time)", "title" : "News item 3" }, 
    { "id" : "0002", "date" : "Sun Dec 20 2009 12:02:00 GMT-0500 (Eastern Standard Time)", "title" : "News item 2" }, 
    { "id" : "0001", "date" : "Sun Dec 19 2009 12:01:00 GMT-0500 (Eastern Standard Time)", "title" : "News item 1" } 
] 
}) 

CSS(同前)

HTML

<div id="wrapper"> 
<div class="column"><div class="top">Content added below</div></div> 
</div> 

腳本

$(document).ready(function(){ 
initData(); 
}) 

function initData(){ 
var o = { 
    maxRows: 10, // max # of rows to show 
    updateJSON : 15, // update data every 30 seconds 
    updateScreen : 1, // update screen every 2 seconds with data 

    count: 0, // count # of cycles of setTimeout 
    lastID: -1, // last news ID 
    debug: false // debug 
}; 
var cycle; 
if (o.debug) $('#wrapper').prepend('<div id="debug"></div>') 
getData([]); 

function addRow(content){ 
    // update data if this function has cycled 30/2 times - so we only use one settimeout function 
    o.count++; 
    if (o.debug) $('#debug').html('seconds passed = ' + o.count*o.updateScreen + ', content.length = ' + content.length); 
    if (o.count >= o.updateJSON/o.updateScreen) { 
    o.count = 0; 
    getData(content); 
    } 

    if (content.length > 0) { 
    // remove last array element 
    var n = content.pop(); 
    // if content length is zero, the set last ID 
    if (content.length === 0) o.lastID = n[0]; 
    // add new row, rel contains the id - not used by the script, but there if you need it. 
    $('#wrapper').find('.top').after('<div class="newrow row" rel="' + n[0] + '">' + n[2] + '<br />' + n[1] + '</div>'); 
    $('.newrow').fadeIn('slow', function(){ $(this).removeClass('newrow') }); 
    } 
    $('#wrapper').find('.row:gt(' + (o.maxRows-1) + ')').remove(); 
    cycle = setTimeout(function(){ addRow(content) } , o.updateScreen * 1000); 
} 

function getData(content){ 
    var tmparray = []; 
    var lastIDfound = false; 
    $.ajax({ 
    type: "GET", 
    url: "newsitems.js", 
    dataType: "json", 
    success: function(data) { 
    $.each(data.news, function(news){ 
    if (this.id == o.lastID) lastIDfound = true; 
    if (!lastIDfound){ 
    // return month day year time (assuming US notation) 
     var newsDate = this.date.split(' ').slice(1,5); 
     // get today's month day year 
     var today = new Date().toString().split(' ').slice(1,4).join(' '); 
     // show only the time if the date is today 
     var newsTime = (newsDate.slice(0,3).join(' ') == today) ? 'posted at ' + newsDate[3] : 'from ' + newsDate.join(' '); 
     tmparray.push([ this.id, newsTime, this.title ]); 
    } 
    }); 
    // if tmparray length is zero, then return 
    if (tmparray.length === 0) return; 
    // if last item shown hasn't changed, then return with no new data 
    if (tmparray[0][0] == o.lastID) return; 
    // set last ID 
    if (o.lastID == -1) o.lastID = tmparray[0][0]; 
    clearTimeout(cycle); 
    if (content.length === 0) { 
    addRow(tmparray); 
    } else { 
    addRow(content.concat(tmparray)); 
    } 
    }, 
    error: function(XMLHttpRequest, textStatus, errorThrown) { 
    if (!$('#error').length) $("#wrapper").prepend('<div id="error"></div>'); 
    $('#error').html(errorThrown + '<br />'); 
    } 
    }) 
} 
} 
+0

我想我必須對此進行修改,因爲內容僅在點擊按鈕時才添加。我實際上在尋找類似於monitter網站的自動化內容添加。 – 2009-12-20 10:44:25

+0

嗯,我不知道你是如何得到你的數據。你還想要添加更多列的能力嗎? – Mottie 2009-12-20 10:54:21

+0

多級json數據 – 2009-12-20 11:36:38

0

我打算假設您正在使用AJAX加載數據(通過AJAX獲得新DIV的內容)。 然後,你可以做這樣的事情:

編輯:根據您的評論,你可以使用這樣的事情。我沒有測試的代碼,但邏輯是存在的(只是希望我沒有擺烏龍:)):

var lastAddedTime = null; 

function addNewDiv (response) { 
    var timeNow = new Date().getTime(); 
    if (lastAdded === null || (timeNow - lastAddedTime) > 5000) { 
     // depending on what response is build your DIV 
     // it could be HTML or JSON from which you generate HTML 
     var newDiv = $(response); 
     newDiv.css ('display', 'none'); 

     newDiv.prependTo ($('selector for your container')); 
     newDiv.fadeIn ('slow'); 
     lastAddedTime = timeNow; 
    } else { 
     setTimeout (function() { addNewDiv (response); }, 5001); 
    } 
} 

$.get (
    'get_data.php', 
    { 
     // your parameters 
    }, 
    function (response) { 
     addNewDiv (response); 
    } 
); 

5000毫秒爲5秒,調整,如果您想更新

較短的時間間隔
+0

該方法將快速填充所有數據。如果我想在兩個div之間有時間延遲,以便用戶可以瀏覽每個div的內容,該怎麼辦? – 2009-12-20 11:50:57