|
@ -1,25 +1,36 @@ |
|
|
<tiny-one-page> |
|
|
<tiny-one-page> |
|
|
<div class="tiny-one-page"> |
|
|
<div class="tiny-one-page"> |
|
|
<div class="tiny-one-page__inner"></div> |
|
|
<div class="tiny-one-page__inner"></div> |
|
|
|
|
|
<button class="tiny-one-page__button"> |
|
|
|
|
|
<i class="icon icon-menu"></i> |
|
|
|
|
|
</button> |
|
|
</div> |
|
|
</div> |
|
|
|
|
|
|
|
|
<script> |
|
|
<script> |
|
|
|
|
|
|
|
|
import SmoothScroll from 'smooth-scroll'; |
|
|
import SmoothScroll from 'smooth-scroll'; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* tiny-one-page.riot |
|
|
|
|
|
* |
|
|
|
|
|
* Tiny Component to one page navigation |
|
|
|
|
|
* |
|
|
|
|
|
* <tiny-one-page> |
|
|
|
|
|
* <ul> |
|
|
|
|
|
* <li><a href="#home">Home</a></li> |
|
|
|
|
|
* <li><a href="#blog">Blog</a></li> |
|
|
|
|
|
* <li><a href="#contact">Contact</a></li> |
|
|
|
|
|
* </ul> |
|
|
|
|
|
* </tiny-one-page> |
|
|
|
|
|
* |
|
|
|
|
|
* @author Björn Hase |
|
|
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License |
|
|
|
|
|
* @link https://gitlab.tentakelfabrik.de/tentakelfabrik/tiny-components/tiny-one-page Gitlab Repository |
|
|
|
|
|
* |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
export default |
|
|
export default |
|
|
{ |
|
|
{ |
|
|
/** |
|
|
|
|
|
* |
|
|
|
|
|
* |
|
|
|
|
|
*/ |
|
|
|
|
|
state: |
|
|
|
|
|
{ |
|
|
|
|
|
options: { |
|
|
|
|
|
offset: 100 |
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* getting innerHTML and remove |
|
|
* getting innerHTML and remove |
|
|
* |
|
|
* |
|
@ -43,31 +54,18 @@ |
|
|
wrapper.innerHTML = this.content; |
|
|
wrapper.innerHTML = this.content; |
|
|
|
|
|
|
|
|
// adding scroll smooth, get all elements with hash |
|
|
// adding scroll smooth, get all elements with hash |
|
|
this.scroll = new SmoothScroll('.tiny-one-page__inner a[href*="#"]', { |
|
|
|
|
|
offset: function() { |
|
|
|
|
|
return this.state.options.offset; |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
this.scroll = new SmoothScroll('.tiny-one-page__inner a[href*="#"]'); |
|
|
|
|
|
|
|
|
window.addEventListener('scroll', this.handleScroll.bind(this), false); |
|
|
window.addEventListener('scroll', this.handleScroll.bind(this), false); |
|
|
|
|
|
|
|
|
|
|
|
// if hash is set, |
|
|
if (window.location.hash) { |
|
|
if (window.location.hash) { |
|
|
this.addClass(this.$(".tiny-one-page__inner a[href$='" + window.location.hash + "']")); |
|
|
|
|
|
|
|
|
this.$(".tiny-one-page__inner a[href$='" + window.location.hash + "']").click(); |
|
|
} else { |
|
|
} else { |
|
|
this.addClass(this.$$(".tiny-one-page__inner a")[0]); |
|
|
this.addClass(this.$$(".tiny-one-page__inner a")[0]); |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* |
|
|
|
|
|
* @param {Object} element |
|
|
|
|
|
* @return {Boolean} |
|
|
|
|
|
*/ |
|
|
|
|
|
isVisible(element) |
|
|
|
|
|
{ |
|
|
|
|
|
return ((element.offsetHeight + element.offsetTop) - this.state.options.offset) > window.pageYOffset; |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* add css class to parent element |
|
|
* add css class to parent element |
|
|
* |
|
|
* |
|
@ -95,7 +93,7 @@ |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* handle scrolling, |
|
|
|
|
|
|
|
|
* handle scrolling and check visibility of elements from navigation |
|
|
* |
|
|
* |
|
|
* @param {Object} event |
|
|
* @param {Object} event |
|
|
*/ |
|
|
*/ |
|
@ -107,17 +105,48 @@ |
|
|
// if found |
|
|
// if found |
|
|
let hasFound = false; |
|
|
let hasFound = false; |
|
|
|
|
|
|
|
|
|
|
|
// get result |
|
|
|
|
|
let result = undefined; |
|
|
|
|
|
|
|
|
elements.forEach(function(element, index) { |
|
|
elements.forEach(function(element, index) { |
|
|
|
|
|
|
|
|
const target = document.querySelector(element.hash); |
|
|
|
|
|
|
|
|
// getting target |
|
|
|
|
|
let target = document.querySelector(element.hash); |
|
|
|
|
|
|
|
|
|
|
|
// check if there is a next element |
|
|
|
|
|
let next = undefined; |
|
|
|
|
|
|
|
|
|
|
|
// getting offsetTop |
|
|
|
|
|
let offsetTop = target.offsetTop; |
|
|
|
|
|
|
|
|
|
|
|
// check for next element |
|
|
|
|
|
if (elements[index + 1]) { |
|
|
|
|
|
next = document.querySelector(elements[index + 1].hash); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (index === 0) { |
|
|
|
|
|
offsetTop = 0; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// check if element is visible |
|
|
|
|
|
if (!result && ((offsetTop <= window.pageYOffset) && (offsetTop + target.offsetHeight) > window.pageYOffset)) { |
|
|
|
|
|
result = target; |
|
|
|
|
|
|
|
|
if (this.isVisible(target) && hasFound === false) { |
|
|
|
|
|
this.addClass(element); |
|
|
|
|
|
hasFound = true; |
|
|
|
|
|
} else { |
|
|
|
|
|
this.removeClass(element); |
|
|
|
|
|
|
|
|
// check if the next element is showing up |
|
|
|
|
|
if (next && window.pageYOffset > offsetTop && next.offsetTop > window.pageYOffset && next.offsetTop < (window.pageYOffset + 800)) { |
|
|
|
|
|
result = next; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// remove class from each element |
|
|
|
|
|
this.removeClass(element); |
|
|
|
|
|
|
|
|
}.bind(this)); |
|
|
}.bind(this)); |
|
|
|
|
|
|
|
|
|
|
|
// @TODO small workaround to ignore gaps between elements where no one found |
|
|
|
|
|
if (result) { |
|
|
|
|
|
this.addClass(this.$(".tiny-one-page__inner a[href$='" + result.id + "']")); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|