working on home scroll

This commit is contained in:
infeeeee
2019-07-18 03:16:06 +02:00
parent 4c443cd7ec
commit 0f3aaed434
2 changed files with 113 additions and 18 deletions

View File

@@ -342,6 +342,7 @@
//scroll to the next or prev article
function homeScroll(target, direction) {
// console.log(target.attr('class'))
var nextelem;
if (direction == "next") {
@@ -354,9 +355,9 @@
}
var offset = ($(nextelem).position().left)
//if loadmore is the next, do not scroll, but load
if ($(nextelem).attr("class") == "misha_loadmore") {
//if loadmore is the next, do not scroll, but load:
if (nextelem.attr("class") == "misha_loadmore") {
loadmore()
return target
} else {
@@ -399,11 +400,35 @@
//checking the current scroll location for refresh:
if (!isMobile) {
if (isHome || isArchive) {
var articleWidth = $(selector + ':first-child')[0].getBoundingClientRect().width
const homeDivWidths = []
const homeFullWidths = []
homeDivWidths[0] = 0
$(selector).each(function () {
homeDivWidths.push($(this)[0].getBoundingClientRect().width)
})
for (let i = 0; i < homeDivWidths.length; i++) {
let prev = homeFullWidths[i - 1]
if (i == 0) {
prev = 0
}
homeFullWidths[i] = homeDivWidths[i] + prev
}
console.log(homeDivWidths)
console.log(homeFullWidths)
var currscroll = $('html').scrollLeft()
var scrollnr = Math.floor(currscroll / articleWidth) + 1
var closest = homeFullWidths.reduce(function (prev, curr) {
return (Math.abs(curr - currscroll) < Math.abs(prev - currscroll) ? curr : prev);
});
var scrollnr = homeFullWidths.indexOf(closest) + 1
console.log("currscroll", currscroll, "scrollnr", scrollnr, "closest", closest)
currElem = $(selector + ':nth-child(' + scrollnr + ')')
console.log("start location: " + currElem.attr("class"))
}
}
@@ -441,20 +466,53 @@
})
/* --------------------------- scroll with keyboard --------------------------- */
$('html *:not(input)').keydown(function () {
var kd = false
$('html *:not(input)').keydown(function (event) {
if (kd) {
event.preventDefault();
return
} else {
if (!isMobile) {
if (isHome || isArchive) {
if (!$('.search-bar *').is(':focus')) {
// console.log("keydown!")
kd = true
switch (event.which) {
case 40: //down
case 39: //right
case 74: //J
event.preventDefault();
currElem = homeScroll(currElem, "next")
break;
case 38: //up
case 37: //left
case 75: //K
event.preventDefault();
currElem = homeScroll(currElem, "prev")
break;
default:
break;
}
}
}
}
}
})
//check for keyup for disabling counter
$('html *:not(input)').keyup(function (event) {
if (!isMobile) {
if (isHome || isArchive) {
if (!$('.search-bar *').is(':focus')) {
//right and J
if (event.which == 39 || event.which == 74) {
event.preventDefault();
currElem = homeScroll(currElem, "next")
// left and K:
} else if (event.which == 37 || event.which == 75) {
currElem = homeScroll(currElem, "prev")
}
kd = false
}
}
}
})
@@ -501,7 +559,7 @@
'query': misha_loadmore_params.posts, // that's how we get params from wp_localize_script() function
'page': misha_loadmore_params.current_page
};
button.trigger("moreload-start")
// button.trigger("moreload-start")
$.ajax({ // you can also use $.post here
url: misha_loadmore_params.ajaxurl, // AJAX handler
@@ -522,7 +580,7 @@
button.html('<a>Mutass többet</a>').before(data); // insert new posts
misha_loadmore_params.current_page++;
button.trigger("moreload-finish")
console.log(data.length)
console.log("loading:", data.length)
// you can also fire the "post-load" event here if you use a plugin that requires it
// $( document.body ).trigger( 'post-load' );