Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am writing a webpage and use included PHP files to make the source code simpler.

Here is the code:

Filename: question.php

HTML
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

<?php include 'questionmenu.php';?>
<?php include 'questionbanner.php';?>
<?php include 'questionmaincontainer.php';?>

</body>

</html>


There is a button in the questionmenu.php file. The code is the following:

<pre lang="PHP">
<style>
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.questionmenu {
    width: 100%;
    height: 80px;
    border: 10px solid blue;
}
.button {
    height: 100%;
    border: 10px solid red;
    color: black;
}
.button:hover {
    color: white;
    background-color: red;
}
</style>
<div class="questionmenu">
    <button onclick="myFunction(); scrollWin()" class="button">PUSH TO SCROLL</button>
</div>

<script>
function myFunction() {
  var elmnt = document.getElementById("scrolluntil");
  elmnt.scrollIntoView();
}
</script>

<script>
function scrollWin() {
  window.scrollBy(0, 800);
}
</script>


There is a questionbanner.php file to make the height of the page. There is no other function. The code is simple like this:

PHP
<style>
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.questionbanner {
    width: 100%;
    height: 1000px;
    border: 10px solid green;
}
</style>
<div class="questionbanner"></div>


At last, there is the questionmaincontainer.php file. It has three columns and I want to scroll down to the column-center div p that's ID is "scrolluntil" when I push the button.

PHP
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<style>
* {
padding: 0;
margin: 0;
}
.column-left{
    float: left;
    width: 25%;
    height: 400px;
    border: 10px solid grey;
}
.column-center{
    float: left;
    width: 50%;
    height: 400px;
    border: 10px solid lightblue;
}
.column-right{
    float: left;
    width: 25%;
    height: 400px;
    border: 10px solid yellow;
}
</style>

<div class="container">
   <div class="column-left"></div>

   <div class="column-center">
       <p id="scrolluntil">SCROLL DOWN UNTIL THIS!</p>
   </div>

   <div class="column-right"></div>
</div>


What I have tried:

The scripts are in the questionmenu.php file. The scripts are the following:

JavaScript
function myFunction() {
  var elmnt = document.getElementById("scrolluntil");
  elmnt.scrollIntoView();
}


and

JavaScript
function scrollWin() {
  window.scrollBy(0, 800);
}


None of the scripts work.
The problem may be that the questionmenu.php height is about 80px and the scroll should be in the question.php file. I do not know how to solve this problem to work these scripts from an included file to make effect outside of it. Does anybody meet a similar problem?
Posted
Comments
Richard Deeming 13-Jul-21 5:14am    
The scripts from your question work fine in a standalone page. Check your browser's developer tools for error messages.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900