Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make colorbox content scrollable #119

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 81 additions & 0 deletions assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,87 @@ $(document).ready(function () {

var active_group = $('.group_tabs li.active a').attr('class');
$("#" + active_group + ".file_list_container").show();
},
onComplete: function (a, b, c) {
var hash = a.el.hash
var sourceCode = $(hash)
var minDistance = 0
var maxDistance
var sourceCodeHeight = sourceCode.height();
if (sourceCodeHeight > 500) {
maxDistance = sourceCodeHeight - 500
} else {
maxDistance = sourceCodeHeight
}
var step = 22
var distance = step * 4
var onScroll = function(event) {
distance = $(hash).position().top * -1
console.log('set distance', distance)
}
var $parentDiv = $('#cboxLoadedContent')

$parentDiv.on('scroll', onScroll)

var scrollDown = function () {
$parentDiv.off('scroll')
if (distance > maxDistance) {
return
} else if (distance < step) {
distance = step
}
console.log('down', distance, maxDistance)

$parentDiv.animate({ scrollTop: distance }, { duration: 0, complete: function () {
if (distance + step > maxDistance) {
distance = maxDistance;
} else {
distance += step;
}

setTimeout(function() {
$parentDiv.on('scroll', onScroll)
}, 1000)
}});
}

var scrollUp = function () {
console.log('up', distance, minDistance, maxDistance)
$parentDiv.off('scroll')
if (distance == 0) {
return
} else if (distance <= minDistance + 100) {
distance = 0
} else if (distance == maxDistance) {
distance -= (step * 2)
}

$parentDiv.animate({ scrollTop: distance }, { duration: 0, complete: function () {
console.log('up done', distance)
if (distance + 100 < minDistance) {
distance = minDistance
} else {
distance -= step;
}
setTimeout(function() {
$parentDiv.on('scroll', onScroll)
}, 1000)
}});
}

$('#colorbox').on('keydown', function (evt) {
if (evt.keyCode == 40) { // down arrow
evt.preventDefault(); // prevents the usual scrolling behaviour
scrollDown()
} else if (evt.keyCode == 38) { // up arrow
evt.preventDefault();
scrollUp()
}
})
},
onClosed: function (a, b, c) {
$('#colorbox').off('keydown')
$('#cboxLoadedContent').off('scroll')
}
});

Expand Down