Skip to content

Commit

Permalink
✨🧲 feat: allow fixed/sticky positioning in config
Browse files Browse the repository at this point in the history
  • Loading branch information
niketpathak committed Aug 9, 2022
1 parent 07608e0 commit 2af44a9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ Ad-rotator accepts the following configuration options and all of them are **Opt
sticky: {
beforeEl: document.querySelector('.start'),
afterEl: document.querySelector('.end'),
position: 'sticky', // or 'fixed' (defaults to 'fixed')
offsetTop: '10', // or '10px' (defaults to 0px)
offsetBottom: '150px', // or '150' (defaults to 0px)
noMobile: true // disable stickiness on mobile (defaults to false)
Expand Down
12 changes: 10 additions & 2 deletions demo/dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,17 @@
.end {
height: 1000px;
}

/* #publicity {
position: sticky;
top: 20;
} */
</style>
<main class="start">Main Content</main>
<div id="publicity">Ad area</div>
<section class="middle">Middle content</section>
<div class="container">
<div id="publicity">Ad area</div>
<section class="middle">Middle content</section>
</div>
<footer class="end">Footer content</footer>

<script>
Expand All @@ -45,6 +52,7 @@
sticky: {
beforeEl: document.querySelector('.start'),
afterEl: document.querySelector('.end'),
position: 'sticky',
offsetTop: '20', // or '20px' (defaults to 0px)
offsetBottom: '10px', // or '30' (defaults to 0px)
noMobile: true // disable stickiness on mobile (default - false)
Expand Down
8 changes: 7 additions & 1 deletion docs/pages/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,9 @@ <h4><a href="#config?id=sticky" id="sticky" title="sticky config param - Ad-rota
sticky: {
beforeEl: document.querySelector('.start'),
afterEl: document.querySelector('.end'),
position: 'fixed', // default - 'fixed'
offsetTop: '20', // or '20px' (defaults to 0px)
offsetBottom: '100px', // or '100' (defaults to 0px)
offsetBottom: '100px', // or '100' (defaults to 0px)
noMobile: true // disable stickiness on mobile (default - false)
},
}
Expand Down Expand Up @@ -474,6 +475,11 @@ <h4><a href="#config?id=sticky" id="sticky" title="sticky config param - Ad-rota
i.e. when you scroll to/after this element, the Ad will no longer be sticky.
By default, the Ad stays sticky permanently.
</li>
<li>
<code>position</code>: <i>"fixed" | "sticky"</i> - refers to the positioning to be used.
By default, <b>fixed</b> positioning is used. When using <b>sticky</b> positioning,
the <i>beforeEl</i>, <i>afterEl</i> and <i>offsetBottom</i> options have no effect.
</li>
<li>
<code>offsetTop</code>: <i>number | string</i> - A margin that is added at the top of the Ad.
Defaults to <b>0</b> i.e. sticks to the top of the screen.
Expand Down
13 changes: 9 additions & 4 deletions src/ad-rotator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ const detectBlock = async () => {
export const stickyEl = (El: HTMLElement, stickyConf: StickyConfig): null | (() => void) => {
if (!El || !(El instanceof HTMLElement) || !stickyConf || stickyConf.constructor !== Object) return null;

const { beforeEl, afterEl, offsetTop, offsetBottom } = stickyConf;
const { beforeEl, afterEl, offsetTop, offsetBottom, position } = stickyConf;
const isStickyPos = position === 'sticky' ? 1 : 0;

let startPos = 0,
endPos = 0;
let ticking = false;
Expand All @@ -78,11 +80,14 @@ export const stickyEl = (El: HTMLElement, stickyConf: StickyConfig): null | (()
!(endPos && scrollPos > endPos - El.clientHeight - (parseInt(offsetBottom as string, 10) || 0))
) {
El.classList.add('stickyElx');
El.style.position = 'fixed';
El.style.position = isStickyPos ? 'sticky' : 'fixed';
El.style.top = (parseInt(offsetTop as string, 10) || 0) + 'px';
} else {
El.style.top = '0';
El.style.position = 'relative';
if (!isStickyPos) {
// fixed positioning
El.style.top = '0';
El.style.position = 'relative';
}
El.classList.remove('stickyElx');
}
ticking = false;
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface StickyConfig {
afterEl?: HTMLElement;
offsetTop?: string;
offsetBottom?: string;
position?: 'fixed' | 'sticky';
noMobile?: boolean;
}

Expand Down

0 comments on commit 2af44a9

Please sign in to comment.