blixhavn / sticky-sidebar-v2

Pure JavaScript tool for making smart and high performance sticky sidebar.
https://blixhavn.github.io/sticky-sidebar-v2/
MIT License
56 stars 25 forks source link

destroy() not working #20

Open artonomic opened 2 years ago

artonomic commented 2 years ago

Normally I would disable the sticky sidebar for smaller screens (< 992px) by using the "minWidth" option, but I would like to try something different with the destroy function. I have created a fiddle to demonstrate the issue.

https://jsfiddle.net/kynham79/

You will notice that the sticky sidebar is disabled when the document is loaded, and when you resize above 992px it will stick as expected. Once you resize back below 992px, the sticky sidebar should be disabled, but it throws an error. Watch the console.

vstruhar commented 1 year ago

Seems that you need to initialise your sidebar variable outside, so it can be accessed from the scope where you call destroy().

image

electronicdreamer commented 1 year ago

I had a problem with my destroy related to Vue 3 as version 3 uses proxies for everything.

Sticky sidebar makes use of the handleEvent function, which is the reason why the whole class is passed to the addEventListener but in Vue's case, when I called the destroy function, it called it on the proxy and not the real this so it couldn't unbind the event.

So I rewrote some of the code for my use case.