Skip to content

Commit

Permalink
v1.7.3 - More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
iDev-Games committed Mar 30, 2023
1 parent a103e13 commit 9691e6e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 36 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ npm i trig-js

Use Trig.js from a CDN
```
https://cdn.jsdelivr.net/npm/[email protected].2/dist/trig.js
https://cdn.jsdelivr.net/npm/[email protected].3/dist/trig.js
```
```
https://unpkg.com/[email protected].2/dist/trig.js
https://unpkg.com/[email protected].3/dist/trig.js
```

# What is Trig.js?
Expand All @@ -58,10 +58,10 @@ All you need to do is add the dist trig.js file into your projects JS folder and

Or just add one of the below CDN instead
```
<script src="https://unpkg.com/[email protected].2/dist/trig.js"></script>
<script src="https://unpkg.com/[email protected].3/dist/trig.js"></script>
```
```
<script src="https://cdn.jsdelivr.net/npm/[email protected].2/dist/trig.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected].3/dist/trig.js"></script>
```

# How To Use?
Expand Down
2 changes: 1 addition & 1 deletion dist/trig.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,10 @@ <h2>How To Install?</h2>
</pre>
<p>Or just add one of the below CDN instead</p>
<pre>
&lt;script src="https://unpkg.com/[email protected].2/dist/trig.js"&gt;&lt;/script&gt;
&lt;script src="https://unpkg.com/[email protected].3/dist/trig.js"&gt;&lt;/script&gt;
</pre>
<pre>
&lt;script src="https://cdn.jsdelivr.net/npm/[email protected].2/dist/trig.js"&gt;&lt;/script&gt;
&lt;script src="https://cdn.jsdelivr.net/npm/[email protected].3/dist/trig.js"&gt;&lt;/script&gt;
</pre>
</div>
</div>
Expand Down
55 changes: 26 additions & 29 deletions src/trig.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
/* Trig.js v1.7.2 by iDev Games */
/* Trig.js v1.7.3 by iDev Games */
const trig = {
trigs: [],
thePos: [],
documentHeight: 0,
observer: new IntersectionObserver(function(entries) {
trigObject.trigEntries(entries);
trigObject.updatePos(trigObject.trigs);
trigObject.observer.disconnect();
}),
trigIntersecting: function(entry) {
if (entry.isIntersecting) {
entry.target.classList.add("trig");
} else {
entry.target.classList.remove("trig");
}
},
trigInit: function() {
trigObject.trigs = document.querySelectorAll('.enable-trig,[data-trig]');
trigObject.documentHeight = innerHeight;
trigObject.trigScroll();
},
trigScroll: function(){
if (trigObject.trigs) {
trigObject.trigs.forEach(function(element, index) {
element.index = index;
trigObject.observer.observe(element);
});
}
},
trigEntries: function(entries) {
entries.forEach(function(entry) {
trigObject.trigIntersecting(entry);
trigObject.trigPos(entry);
});
},
trigSetPos: function(el, min, max, entry, offset) {
var posTop = 0 - (el - ((trigObject.documentHeight / 2) + offset));
var pos = (posTop / trigObject.documentHeight) * 100;
trigSetPos: function(pos, min, max, entry) {
if (pos >= min && pos <= max) {
trigObject.thePos[entry.target.index] = pos;
} else if (pos <= min) {
Expand All @@ -41,7 +57,9 @@ const trig = {
if (entry.target.dataset.trigMax) {
max = parseInt(entry.target.dataset.trigMax);
}
trigObject.trigSetPos(el, min, max, entry, offset);
var posTop = 0 - (el - ((trigObject.documentHeight / 2) + offset));
var pos = (posTop / trigObject.documentHeight) * 100;
trigObject.trigSetPos(pos, min, max, entry);
},
updatePos: function() {
trigObject.trigs.forEach(function(element, index) {
Expand All @@ -56,29 +74,8 @@ const trig = {
}
};

const observer = new IntersectionObserver(function(entries) {
trigObject.trigEntries(entries);
trigObject.updatePos(trigObject.trigs);
observer.disconnect();
});

const trigObject = Object.create(trig);

document.addEventListener('scroll', trigScroll, false);
document.addEventListener('resize', trigInit, false);
document.addEventListener('DOMContentLoaded', trigInit, false);

function trigInit(){
trigObject.trigs = document.querySelectorAll('.enable-trig,[data-trig]');
trigObject.documentHeight = innerHeight;
trigScroll();
}

function trigScroll() {
if (trigObject.trigs) {
trigObject.trigs.forEach(function(element, index) {
element.index = index;
observer.observe(element);
});
}
}
document.addEventListener('scroll', trigObject.trigScroll, false);
document.addEventListener('resize', trigObject.trigInit, false);
document.addEventListener('DOMContentLoaded', trigObject.trigInit, false);

0 comments on commit 9691e6e

Please sign in to comment.