Strange JS behavior with WP sidebar
-
Hi All,
I am hoping someone can help out with what seems like a very strange behavior in my WP widget.
I will layout some background info.
The widget was developed in javascript. So, it can be used on a non WP site as well. When used outside of WP it consists of a functions file that goes in the header and a div and script tag that is in the body. Pretty standard stuff for a widget:
<!--in the header--> <script src=https://www.mysite.com/js/functions.js></script> <!--in the body--> <div id="123"></div> <script> param1="123"; param1="a"; param1="b"; more params... doWork(); </script>
Here is an overview of what the code does.
The div that is created is the “main” div for the widget. The widget can function one of two ways based on params.
1)It can be inline content. So, wherever you place the div and script tags, the widget will be displayed. The javascript function runs, reads the params, gets the relevent content, and creates an iFrame using document.createElement. The iFrame as appended to the “main” div with appendChild.
2)It can have a floating button (position:fixed;left:0;top:50%). When you click on this button, a new div slides from offscreen. In this case, the “main” div becomes the floating button. Two new divs are created using createElement:
a)a div that is the same dimension and position as the “main” div. it goes just under the “main” div. Its only purpose is so that the “main” div (acting as a button) can have opacity:.6 and you dont see the content underneath because this other div has opacity:1 and color #fff.
b)a div that is positioned offscreen. The iFrame created with createElement is added to this div. When you click the floating button, this div slides onto the screen.
It all works fine in any non-WP site I have tested. No issues at all.
So, now the plan is to make this a WP widget. I enque the functions file (and a css file), add the params as widget options, create the body script tag in echo statements, and test with the Twenty Fifteen template, which has a sidebar–which floats separate from the content of the page. I believe it is called “sticky” as it floats until it hits the top or bottom.
When I test with the widget as inline content (#1 above), no issues. So, everything now is about when the widget is using a floating button.
When I make it a floating button, this is what happens–when you scroll the page and the sidebar reaches the top or bottom, the “main” div disappears. Scroll the other way, away from the top or bottom, and the “main” div appears again. So, it only disappears while the side bar is resting on the top or bottom. If I make the sidebar longer than the content of the page (with more widgets or a bunch of <br>) the “main” div never disappears.
I just can’t make sense out of it. I would think that, if anything strange like this were going to happen, it would be to a div that was added to the DOM using the js. But, this is why I put “main” div in quotes all along–to emphasize it is a real div, not added to the dom by js. It is the div that is created with an echo statement that this is happening to. The divs created by createElement are fine. They never disappear.
I have checked and ruled out any css issues. And, unless I missed something, have checked anything I am doing to this div in the js (really nothing more than adding classes, setting some attributes. And I am moving one
<a>
from the “main” div to the sliding div (this is for seo purposes–so an<a>
to my site exists in the HTML before I move it to the slider div) but I have ruled this out as not an issue.Does this make any sense to anyone? Have you seen it before? Im happy to share pages, css, js, etc for anyone interested.
- This topic was modified 6 years, 9 months ago by .
- This topic was modified 6 years, 9 months ago by .
- This topic was modified 6 years, 9 months ago by .
- This topic was modified 6 years, 9 months ago by .
- This topic was modified 6 years, 9 months ago by .
- This topic was modified 6 years, 9 months ago by .
The page I need help with: [log in to see the link]
- The topic ‘Strange JS behavior with WP sidebar’ is closed to new replies.