Skip to content

Commit

Permalink
v1.9.0 - Reintroduced data-trig-height and reformatted for better com…
Browse files Browse the repository at this point in the history
…pression
  • Loading branch information
iDev-Games committed Apr 4, 2023
1 parent 0730d19 commit e4e0e24
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 79 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ npm i trig-js

Use Trig.js from a CDN
```
https://cdn.jsdelivr.net/npm/trig-js@1.8.0/dist/trig.js
https://cdn.jsdelivr.net/npm/trig-js@1.9.0/dist/trig.js
```
```
https://unpkg.com/trig-js@1.8.0/dist/trig.js
https://unpkg.com/trig-js@1.9.0/dist/trig.js
```

# What is Trig.js?
Expand All @@ -62,10 +62,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
```html
<script src="https://cdn.jsdelivr.net/npm/trig-js@1.8.0/dist/trig.js"></script>
<script src="https://cdn.jsdelivr.net/npm/trig-js@1.9.0/dist/trig.js"></script>
```
```html
<script src="https://unpkg.com/trig-js@1.8.0/dist/trig.js"></script>
<script src="https://unpkg.com/trig-js@1.9.0/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.

6 changes: 3 additions & 3 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://cdn.jsdelivr.net/npm/trig-js@1.8.0/dist/trig.js"&gt;&lt;/script&gt;
&lt;script src="https://cdn.jsdelivr.net/npm/trig-js@1.9.0/dist/trig.js"&gt;&lt;/script&gt;
</pre>
<pre>
&lt;script src="https://unpkg.com/trig-js@1.8.0/dist/trig.js"&gt;&lt;/script&gt;
&lt;script src="https://unpkg.com/trig-js@1.9.0/dist/trig.js"&gt;&lt;/script&gt;
</pre>
</div>
</div>
Expand Down Expand Up @@ -276,7 +276,7 @@ <h2>Data Attributes</h2>
<p>You can use the below data attributes for additional features</p>
<div class="listItem">
<pre>
&lt;div id="yourelement" data-trig-min="-100" data-trig-max="100" data-trig-offset="0" data-trig-global="true" data-trig&gt; &lt;/div&gt;
&lt;div id="yourelement" data-trig-min="-100" data-trig-max="100" data-trig-offset="0" data-trig-height="0" data-trig-global="true" data-trig&gt; &lt;/div&gt;
</pre>
</div>
</div>
Expand Down
18 changes: 15 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "trig-js",
"version": "1.8.0",
"version": "1.9.0",
"description": "The easy way to create CSS scroll animations that react to the position of your HTML element on screen. Animate on scroll (AOS) your CSS.",
"main": "dist/trig.js",
"scripts": {
Expand All @@ -11,10 +11,22 @@
"url": "git+https://github.com/iDev-Games/trig.git"
},
"keywords": [
"Animation",
"trigger",
"position",
"js"
"css",
"html",
"design",
"js",
"frontend",
"simple",
"animation",
"css-animations",
"css-variables",
"scroll-animations",
"data-attribute-html",
"animate-on-scroll",
"reveal-animation",
"aos-animation"
],
"author": "iDev Games",
"license": "MIT",
Expand Down
142 changes: 74 additions & 68 deletions src/trig.js
Original file line number Diff line number Diff line change
@@ -1,91 +1,97 @@
/* Trig.js v1.8.0 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) {
/* Trig.js v1.9.0 by iDev Games */
class Trig
{
trigs = [];
thePos = [];
height = 0;
observer = new IntersectionObserver(function(entries) {
trig.trigEntries(entries);
trig.updatePos(trig.trigs);
trig.observer.disconnect();
});
trigInit() {
trig.trigs = document.querySelectorAll('.enable-trig,[data-trig]');
trig.height = innerHeight;
trig.trigScroll();
}
trigScroll(){
if (trig.trigs) {
trig.trigs.forEach(function(element, index) {
element.index = index;
trigObject.observer.observe(element);
trig.observer.observe(element);
});
}
},
trigEntries: function(entries) {
}
trigEntries(entries) {
entries.forEach(function(entry) {
trigObject.trigIntersecting(entry);
trigObject.trigPos(entry);
trig.trigIntersecting(entry);
trig.trigPos(entry);
});
},
trigSetPos: function(pos, min, max, entry) {
if (pos >= min && pos <= max) {
trigObject.thePos[entry.target.index] = pos;
} else if (pos <= min) {
trigObject.thePos[entry.target.index] = min;
} else if (pos >= max) {
trigObject.thePos[entry.target.index] = max;
}
},
trigPos: function(entry) {
}
trigIntersecting(entry) {
if (entry.isIntersecting) {
entry.target.classList.add("trig");
} else {
entry.target.classList.remove("trig");
}
}
trigPos(entry) {
var offset = 0;
var hOffset = 0;
var min = -100;
var max = 100;
var el = entry.boundingClientRect.top;
var height = entry.boundingClientRect.height;
if (entry.target.dataset.trigOffset) {
offset = parseInt(entry.target.dataset.trigOffset);
var target = entry.target;
var dSet = target.dataset;
if (dSet.trigOffset) {
offset = parseInt(dSet.trigOffset);
}
if (dSet.trigMin) {
min = parseInt(dSet.trigMin);
}
if (entry.target.dataset.trigMin) {
min = parseInt(entry.target.dataset.trigMin);
if (dSet.trigMax) {
max = parseInt(dSet.trigMax);
}
if (entry.target.dataset.trigMax) {
max = parseInt(entry.target.dataset.trigMax);
if (dSet.trigHeight) {
hOffset = parseInt(dSet.trigHeight);
}
if(trigObject.documentHeight > height){
height = trigObject.documentHeight;
if(trig.height > height){
height = trig.height;
}
var posTop = 0 - (el - ((trigObject.documentHeight / 2) + offset));
var pos = (posTop / (height)) * 100;
trigObject.trigSetPos(pos, min, max, entry);
},
updatePos: function() {
trigObject.trigs.forEach(function(element, index) {
if (element.dataset.trigGlobal == "true") {
var posTop = 0 - (el - ((trig.height / 2) + offset));
var pos = (posTop / (height + hOffset)) * 100;
trig.trigSetPos(pos, min, max, target);
}
trigSetPos(pos, min, max, entry) {
if (pos >= min && pos <= max) {
trig.thePos[entry.index] = pos;
} else if (pos <= min) {
trig.thePos[entry.index] = min;
} else if (pos >= max) {
trig.thePos[entry.index] = max;
}
}
updatePos() {
trig.trigs.forEach(function(element, index) {
if (element.dataset.trigGlobal == "true" && element.id) {
var el = document.documentElement.style;
var id = "-"+element.id;
} else {
var el = element.style;
var id = "";
}
el.setProperty('--trig'+id, trigObject.thePos[index] + "%");
el.setProperty('--trig-reverse'+id, -(trigObject.thePos[index]) + "%");
el.setProperty('--trig-px'+id, trigObject.thePos[index] + "px");
el.setProperty('--trig-px-reverse'+id, -(trigObject.thePos[index]) + "px");
el.setProperty('--trig-deg'+id, ((trigObject.thePos[index] / 100) * 360) + "deg");
el.setProperty('--trig-deg-reverse'+id, ((-(trigObject.thePos[index]) / 100) * 360) + "deg");
el.setProperty('--trig'+id, trig.thePos[index] + "%");
el.setProperty('--trig-reverse'+id, -(trig.thePos[index]) + "%");
el.setProperty('--trig-px'+id, trig.thePos[index] + "px");
el.setProperty('--trig-px-reverse'+id, -(trig.thePos[index]) + "px");
el.setProperty('--trig-deg'+id, ((trig.thePos[index] / 100) * 360) + "deg");
el.setProperty('--trig-deg-reverse'+id, ((-(trig.thePos[index]) / 100) * 360) + "deg");
});
}
};

const trigObject = Object.create(trig);
}
const trig = new Trig;

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

0 comments on commit e4e0e24

Please sign in to comment.