Javascript 如何修复使用ajax刷新的页面上的loadmore滚动问题?
我创建了一个使用ajax刷新的照片共享页面。该页面有3个链接(所有照片、高质量照片、低质量照片)。单击其中每一个按钮时,数据(照片)都将使用ajax加载。在滚动之后,ajax提供了更多的照片。每个页面(由于过滤)都有不同的javascript函数,它们是allphotos()和gallery()。两者具有相同的滚动事件功能。向下滚动后,数据开始变得混乱。一个从函数allphoto获取数据,另一个从函数gallery()获取数据我无法解决如何修复此问题。下面是我的javascript和php代码 (代码分为两个主要部分,一个是相互关联的JavaScript代码和php代码) javascript页面(app.js) 在我看来,在加载刷新ajax的spide中,下面的代码很混乱,因为滚动事件已经在同一页面上执行Javascript 如何修复使用ajax刷新的页面上的loadmore滚动问题?,javascript,jquery,ajax,scroll,Javascript,Jquery,Ajax,Scroll,我创建了一个使用ajax刷新的照片共享页面。该页面有3个链接(所有照片、高质量照片、低质量照片)。单击其中每一个按钮时,数据(照片)都将使用ajax加载。在滚动之后,ajax提供了更多的照片。每个页面(由于过滤)都有不同的javascript函数,它们是allphotos()和gallery()。两者具有相同的滚动事件功能。向下滚动后,数据开始变得混乱。一个从函数allphoto获取数据,另一个从函数gallery()获取数据我无法解决如何修复此问题。下面是我的javascript和php代码
$(window).scroll(function (){
if ($(window).scrollTop() + 250 >= $(document).height() - $(window).height() && action == "inactive" && localStorage.getItem("scroll") == "all") {
action = "active";
start_load = start_load + limit_load;
setTimeout(() => {
load_photo_profile(limit_load, start_load);
}, 500);
}
});
尽量缩小问题的范围,把注意力集中在你的问题上。我没能找到一个简单的方法,让你的代码更有条理。
function gallery(){
var limit_load = 5;
var start_load = 0;
var action = "inactive"
function load_photo_profile(limit_load, start_load) {
var url = baseUrl + "exhibition/gallery";
$.ajax({
url: url,
method: "POST",
data: { limit: limit_load, start: start_load },
cache: false,
success: function (response) {
if (response == '') {
$("#included_image_message").html("<h1>No data found</h1>");
action = "active";
} else {
$(".included_image").append(response);
$("#included_image_message").html("<h1>Please Wait</h1>");
action = "inactive";
}
}
});
};
if (action == "inactive") {
action = "active";
load_photo_profile(limit_load, start_load);
}
$(window).scroll(function () {
if ($(window).scrollTop() + 250 >= $(document).height() - $(window).height() && action == "inactive" && localStorage.getItem("scroll") == "all") {
action = "active";
start_load = start_load + limit_load;
setTimeout(() => {
load_photo_profile(limit_load, start_load);
}, 500);
}
});
}
public function loadmore()
{
$limit = $this->input->post("limit");
$start = $this->input->post("start");
$viewData = new StdClass();
$viewData->viewFolder = $this->viewFolder;
$viewData->subViewFolder = "profile";
$get_images = $this->photo_model->get_all_limit(
array(
"Durum" => 1
),
"Id DESC",
$limit,
$start
);
if (!$get_images == "") {
$viewData->items = $get_images;
$activeUser=get_active_user();
$viewData->activeUser = $activeUser;
$render_html = $this->load->view("{$viewData->viewFolder}/assist/mygallery", $viewData, true);
echo $render_html;
}
}
function gallery()
{
$limit = $this->input->post("limit");
$start = $this->input->post("start");
$viewData = new StdClass();
$viewData->viewFolder = $this->viewFolder;
$viewData->subViewFolder = "profile";
$get_images = $this->photo_model->get_all_limit(
array(
"Durum" => 1,
"Tur" => 1
),
"Id DESC",
$limit,
$start
);
if (!$get_images == "") {
$viewData->items = $get_images;
$activeUser = get_active_user();
$viewData->activeUser = $activeUser;
$render_html = $this->load->view("{$viewData->viewFolder}/assist/mygallery", $viewData, true);
echo $render_html;
}
}
$(window).scroll(function (){
if ($(window).scrollTop() + 250 >= $(document).height() - $(window).height() && action == "inactive" && localStorage.getItem("scroll") == "all") {
action = "active";
start_load = start_load + limit_load;
setTimeout(() => {
load_photo_profile(limit_load, start_load);
}, 500);
}
});