Skip to content

Commit

Permalink
feat(scrollbar): allow scrollbar to be enabled/disabled in breakpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
nolimits4web committed May 30, 2022
1 parent d748d49 commit 3f09fc7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
37 changes: 32 additions & 5 deletions src/modules/scrollbar/scrollbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default function Scrollbar({ swiper, extendParams, on, emit }) {
snapOnRelease: true,
lockClass: 'swiper-scrollbar-lock',
dragClass: 'swiper-scrollbar-drag',
scrollbarDisabledClass: 'swiper-scrollbar-disabled',
},
});

Expand Down Expand Up @@ -218,6 +219,7 @@ export default function Scrollbar({ swiper, extendParams, on, emit }) {
function events(method) {
const { scrollbar, touchEventsTouch, touchEventsDesktop, params, support } = swiper;
const $el = scrollbar.$el;
if (!$el) return;
const target = $el[0];
const activeListener =
support.passiveListener && params.passiveListeners
Expand All @@ -241,11 +243,11 @@ export default function Scrollbar({ swiper, extendParams, on, emit }) {
}

function enableDraggable() {
if (!swiper.params.scrollbar.el) return;
if (!swiper.params.scrollbar.el || swiper.scrollbar.el) return;
events('on');
}
function disableDraggable() {
if (!swiper.params.scrollbar.el) return;
if (!swiper.params.scrollbar.el || swiper.scrollbar.el) return;
events('off');
}
function init() {
Expand Down Expand Up @@ -295,9 +297,14 @@ export default function Scrollbar({ swiper, extendParams, on, emit }) {
}

on('init', () => {
init();
updateSize();
setTranslate();
if (swiper.params.navigation.enabled === false) {
// eslint-disable-next-line
disable();
} else {
init();
updateSize();
setTranslate();
}
});
on('update resize observerUpdate lock unlock', () => {
updateSize();
Expand All @@ -318,7 +325,27 @@ export default function Scrollbar({ swiper, extendParams, on, emit }) {
destroy();
});

const enable = () => {
swiper.$el.removeClass(swiper.params.scrollbar.scrollbarDisabledClass);
if (swiper.scrollbar.$el) {
swiper.scrollbar.$el.removeClass(swiper.params.scrollbar.scrollbarDisabledClass);
}
init();
updateSize();
setTranslate();
};

const disable = () => {
swiper.$el.addClass(swiper.params.scrollbar.scrollbarDisabledClass);
if (swiper.scrollbar.$el) {
swiper.scrollbar.$el.addClass(swiper.params.scrollbar.scrollbarDisabledClass);
}
destroy();
};

Object.assign(swiper.scrollbar, {
enable,
disable,
updateSize,
setTranslate,
init,
Expand Down
4 changes: 4 additions & 0 deletions src/modules/scrollbar/scrollbar.less
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
position: relative;
-ms-touch-action: none;
background: rgba(0, 0, 0, 0.1);
.swiper-scrollbar-disabled > &,
&.swiper-scrollbar-disabled {
display: none !important;
}
.swiper-horizontal > & {
position: absolute;
left: 1%;
Expand Down
4 changes: 4 additions & 0 deletions src/modules/scrollbar/scrollbar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
position: relative;
-ms-touch-action: none;
background: rgba(0, 0, 0, 0.1);
.swiper-scrollbar-disabled > &,
&.swiper-scrollbar-disabled {
display: none !important;
}
.swiper-horizontal > & {
position: absolute;
left: 1%;
Expand Down
7 changes: 7 additions & 0 deletions src/types/modules/scrollbar.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,11 @@ export interface ScrollbarOptions {
* @default 'swiper-scrollbar-drag'
*/
dragClass?: string;

/**
* CSS class name added on swiper container and scrollbar element when scrollbar is disabled by breakpoint
*
* @default 'swiper-scrollbar-disabled'
*/
scrollbarDisabledClass?: string;
}

0 comments on commit 3f09fc7

Please sign in to comment.