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

New Public Functions and Events #122

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,36 @@ <h3>Side by side</h3>
<script src="js/jquery.twentytwenty.js"></script>
<script>
$(function(){
$(".twentytwenty-container[data-orientation!='vertical']").twentytwenty({default_offset_pct: 0.7});
$(".twentytwenty-container[data-orientation='vertical']").twentytwenty({default_offset_pct: 0.3, orientation: 'vertical'});
var horizontial2020s = $(".twentytwenty-container[data-orientation!='vertical']").twentytwenty({default_offset_pct: 0.7})
var vertical2020s = $(".twentytwenty-container[data-orientation='vertical']").twentytwenty({default_offset_pct: 0.3, orientation: 'vertical'})

$(horizontial2020s).each((index, twt)=>{
console.log(twt)
twt.onMoveStart = function(e, x){
console.log('onMoveStart', e, x)
}
// twt.onMove = function(e, x){
// console.log('onMove', e, x)
// }
twt.onMoveEnd = function(e, x){
console.log('onMoveEnd', e, x)
}
})
$(vertical2020s).each((index, twt)=>{
console.log(twt)
twt.onMoveStart = function(e, x){
console.log('onMoveStart', e, x)
}
// twt.onMove = function(e, x){
// console.log('onMove', e, x)
// }
twt.onMoveEnd = function(e, x){
console.log('onMoveEnd', e, x)
}
})

});
</script>

</body>
</html>
30 changes: 24 additions & 6 deletions js/jquery.twentytwenty.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

var sliderPct = options.default_offset_pct;
var container = $(this);
var target = container[0];
var sliderOrientation = options.orientation;
var beforeDirection = (sliderOrientation === 'vertical') ? 'down' : 'left';
var afterDirection = (sliderOrientation === 'vertical') ? 'up' : 'right';
Expand All @@ -37,7 +38,7 @@
container.addClass("twentytwenty-container");
beforeImg.addClass("twentytwenty-before");
afterImg.addClass("twentytwenty-after");

var calcOffset = function(dimensionPct) {
var w = beforeImg.width();
var h = beforeImg.height();
Expand Down Expand Up @@ -67,6 +68,8 @@
adjustContainer(offset);
};

this.adjustSlider = adjustSlider;

// Return the number specified or the min/max number if it outside the range given.
var minMaxNumber = function(num, min, max) {
return Math.max(min, Math.min(max, num));
Expand All @@ -90,7 +93,12 @@
var offsetY = 0;
var imgWidth = 0;
var imgHeight = 0;

var onMoveStart = function(e) {
if (target && target.onMoveStart) {
sliderPct = getSliderPercentage(e.pageX, e.pageY);
target.onMoveStart(e, sliderPct)
}
if (((e.distX > e.distY && e.distX < -e.distY) || (e.distX < e.distY && e.distX > -e.distY)) && sliderOrientation !== 'vertical') {
e.preventDefault();
}
Expand All @@ -100,17 +108,26 @@
container.addClass("active");
offsetX = container.offset().left;
offsetY = container.offset().top;
imgWidth = beforeImg.width();
imgHeight = beforeImg.height();
imgWidth = beforeImg.width();
imgHeight = beforeImg.height();
};

var onMove = function(e) {
sliderPct = getSliderPercentage(e.pageX, e.pageY);
if (container.hasClass("active")) {
sliderPct = getSliderPercentage(e.pageX, e.pageY);
adjustSlider(sliderPct);
}
if (target && target.onMove) {
target.onMove(e, sliderPct)
}
};
var onMoveEnd = function() {
container.removeClass("active");

var onMoveEnd = function(e) {
container.removeClass("active");
if (target && target.onMoveEnd) {
sliderPct = getSliderPercentage(e.pageX, e.pageY);
target.onMoveEnd(e, sliderPct)
}
};

var moveTarget = options.move_with_handle_only ? slider : container;
Expand Down Expand Up @@ -145,6 +162,7 @@
}

$(window).trigger("resize.twentytwenty");
return this;
});
};

Expand Down