[英]AJAX load and page scrolling
我需要以下問題的幫助。 我的頁面上有一個按鈕,按下按鈕會通過AJAX將內容加載到位於按鈕下方的“ div”元素中。 一切正常,但只有一件事。 每按一下該按鈕,頁面就會滾動一點,但我希望它保持其位置。
這是我的HTML代碼:
<input type="button" name="calculate" value="Calculate"
onclick="invoke(this.form, this.name, '#result')"/>
這是我的JavaScript代碼(我正在使用jQuery):
function invoke(form, event, container) {
$('input[type="button"]').attr('disabled', 'disabled');
$(container).html('<br/><div class="img"><img src="/Test/img/ajax-loader.gif"/><div>');
$(container).load(form.action, event + '&' + $(form).serialize());
}
我搜索了其他帖子,但找不到任何有效的解決方案。 任何建議,將不勝感激!
我找出問題的根源。 由於每次按下按鈕時,加載到“ div”元素中的內容的高度都會發生2倍的變化,因此頁面主體的高度也會發生變化。 這就是滾動的原因。 我固定了css文件中“ div”元素的高度:
div#result {height: 200px;}
這樣就解決了問題。
很好,對於答案,如果在jsfiddle中提供代碼,也很好。 仍然沒有問題,最近我做了同樣的事情。
默認情況下,如果您單擊按鈕,則其位置相同,但是如果您具有錨標記,則它將自動位於頂部。
有很多東西要停留或滾動到所需的位置。
function buttonclick(isfirsttimeload)
{
if (typeof isfirsttimeload!= "undefined")
window.scrollTo(0, 0);
else
location.hash = '#asearchresult'; //this is hidden anchortag's id, which scrolldown on click.
}
您必須在頁面加載時調用此函數。
limit – The number of records to display per request.
offset – The starting pointer of the data.
busy – Check if current request is going on or not.
The main trick for this scroll down pagination is binding the window scroll event and checking with the data container height
$(document).ready(function() {
$(window).scroll(function() {
// make sure u give the container id of the data to be loaded in.
if ($(window).scrollTop() + $(window).height() > $("#results").height() && !busy) {
busy = true;
offset = limit + offset;
displayRecords(limit, offset);
}
});
})
<script type="text/javascript">
var limit = 10
var offset = 0;
function displayRecords(lim, off) {
$.ajax({
type: "GET",
async: false,
url: "getrecords.php",
data: "limit=" + lim + "&offset=" + off,
cache: false,
beforeSend: function() {
$("#loader_message").html("").hide();
$('#loader_image').show();
},
success: function(html) {
$('#loader_image').hide();
$("#results").append(html);
if (html == "") {
$("#loader_message").html('<button data-atr="nodata" class="btn btn-default" type="button">No more records.</button>').show()
} else {
$("#loader_message").html('<button class="btn btn-default" type="button">Load more data</button>').show();
}
}
});
}
$(document).ready(function() {
// start to load the first set of data
displayRecords(limit, offset);
$('#loader_message').click(function() {
// if it has no more records no need to fire ajax request
var d = $('#loader_message').find("button").attr("data-atr");
if (d != "nodata") {
offset = limit + offset;
displayRecords(limit, offset);
}
});
});
</script>
用php實現,即getrecords.php
<?php
require_once("config.php");
$limit = (intval($_GET['limit']) != 0 ) ? $_GET['limit'] : 10;
$offset = (intval($_GET['offset']) != 0 ) ? $_GET['offset'] : 0;
$sql = "SELECT countries_name FROM countries WHERE 1 ORDER BY countries_name ASC LIMIT $limit OFFSET $offset";
try {
$stmt = $DB->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll();
} catch (Exception $ex) {
echo $ex->getMessage();
}
if (count($results) > 0) {
foreach ($results as $res) {
echo '<h3>' . $res['countries_name'] . '</h3>';
}
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.