\$\begingroup\$
\$\endgroup\$
2
Write a simple monitoring navigation function.
- I feel logic judgment still needs to be optimized.
- I don't have a clear idea right now about code logic such as reducing
if
statements - How can I optimize the performance of this code?
<!-- =panel: start -->
<div class="panel" role="region"></div>
<!-- =panel: end -->
<script>
var em = document.querySelectorAll(".panel");
var viewHeight = window.innerHeight;
var clientHeight = document.body.clientHeight;
var timeOut = null;
em.forEach(function (em, index) {
watchNav(em);
});
function watchNav(em) {
var emHeight = em.offsetHeight;
var emTop = em.offsetTop;
clearTimeout(timeOut);
window.addEventListener("scroll", function (e) {
var scrollY = window.scrollY;
timeOut = setTimeout(function () {
// Logical judgment: how to optimize, want better implementation.
// Such as reducing the if statement
// ++++++++++++++++
if (scrollY > emTop || scrollY + viewHeight / 2 > emTop || scrollY + viewHeight > emTop + emHeight) {
console.log("...activing...");
em.classList.add("view-focus");
}
else {
console.log("none");
em.classList.remove("view-focus");
}
if (scrollY > emTop + emHeight / 2) {
console.log("none");
em.classList.remove("view-focus");
}
// ++++++++++++++++
}, 40);
}, false);
}
</script>
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
1 Answer 1
\$\begingroup\$
\$\endgroup\$
2
A few comments:
Array.forEach
takes a callback, this can be done directly as so:ems.forEach(watchNav)
- Also, you have scope conflicts: the global variable
em
, and the function parameterem
- A third comment: You should n ot edit your question based on feedback in comments.
- Your if statement is fine.
<script>
var ems = document.querySelectorAll(".panel"),
viewHeight = window.innerHeight,
clientHeight = document.body.clientHeight,
timeOut = null;
ems.forEach(watchNav);
function watchNav(em) {
var emHeight = em.offsetHeight;
var emTop = em.offsetTop;
clearTimeout(timeOut);
window.addEventListener("scroll", function (e) {
var scrollY = window.scrollY;
timeOut = setTimeout(function () {
// Logical judgment: how to optimize, want better implementation.
// Such as reducing the if statement
// ++++++++++++++++
if (scrollY > emTop || scrollY + viewHeight / 2 > emTop || scrollY + viewHeight > emTop + emHeight) {
console.log("...activing...");
em.classList.add("view-focus");
} else {
console.log("none");
em.classList.remove("view-focus");
}
if (scrollY > emTop + emHeight / 2) {
console.log("none");
em.classList.remove("view-focus");
}
// ++++++++++++++++
}, 40);
}, false);
}
</script>
```
answered Jan 9, 2019 at 15:58
-
\$\begingroup\$ Thank you for your reply. I want to make the code more optimized. \$\endgroup\$Phoenix– Phoenix2019年01月10日 03:44:24 +00:00Commented Jan 10, 2019 at 3:44
-
2\$\begingroup\$ I would argue that a better way to thank someone is to up-vote the answer. \$\endgroup\$dfhwze– dfhwze2019年10月11日 04:29:50 +00:00Commented Oct 11, 2019 at 4:29
Explore related questions
See similar questions with these tags.
default
.querySelectorAll()
has a.forEach()
method.Array.prototype.slice()
is not necessary. See also Intersection Observer API \$\endgroup\$