I'm making a Russian language dictionary in which entries for keywords are inserted into accordion elements. The information inside the accordion is taken from a PHPmyadmin database. So far so good: I managed to set the database and the accordion mechanism, and as such, the new content is correctly adjusted to the index.php page whenever I add new information to the database.
However, if I click the accordion item at the bottom of the screen, I need to scroll down manually as far as the content box with the definitions of the keyword. As follows:
Here's my abbreviated code:
CSS:
.accordionWrapper { (...) }
.accordionItem { (...) }
.accordionItemHeading {
cursor: pointer; (...)
}
.close .accordionItemContent {
height:0px;
-webkit-transform: scaleY(0);
float:left;
display:block;
}
.open .accordionItemContent {
(...)
display:block;
-webkit-transform: scaleY(1);
-webkit-transform-origin: top;
box-sizing: border-box;
}
.open .accordionItemHeading {
margin:0px;
-webkit-border-top-left-radius: 3px;
-webkit-border-top-right-radius: 3px;
-webkit-border-bottom-right-radius: 0px;
-webkit-border-bottom-left-radius: 0px;
(...)
}
(...)
HTML/PHP
<div class="accordionWrapper">
<div class='accordionItem close'>
<h2 class='accordionItemHeading'> <!--PHP здесь--> </h2>
<div class='accordionItemContent'>
<!--PHP здесь -->
</div>
</div>
</div>
Javascript:
<script>
var accItem = document.getElementsByClassName('accordionItem');
var accHD = document.getElementsByClassName('accordionItemHeading');
var accCon = document.getElementsByClassName('accordionItemContent');
for (i = 0; i < accHD.length; i++) {
accHD[i].addEventListener('click', toggleItem, false);
}
function toggleItem() {
var itemClass = this.parentNode.className;
for (i = 0; i < accItem.length; i++) {
accItem[i].className = 'accordionItem close';
}
if (itemClass == 'accordionItem close') {
this.parentNode.className = 'accordionItem open';
accItem[i].scrollIntoView();
}
}
</script>
Inside the if
statement I tried to type accItem[i].scrollIntoView();
, but no use. Whatever I could find on the web related the scrollIntoview command was about scrolling to a given id by clicking on a navigation button somewhere else. I just want the screen to automatically scroll down to the accordion item which has just been clicked. I don't need any animation or smooth transition.
Thank you for your help in advance.
item heading tag, but if so, it doesn't work either. I guess that is used to link to an element with a specific ID. But my accordion items are described by classes and not ID's.
– swrutra Apr 17 '19 at 14:35