Issue with scrolling feature in div

I am currently facing an issue with scrolling on my website. Visit this site to see the problem. When scrolling down, it causes the "hidden" part of the site to slide up. I want to achieve a similar sliding effect, but it needs to be smooth. I have attempted to use scrollTop() and animate(), but they do not seem to work for me. I am utilizing JQuery for this task. Below is the code I have implemented:

HTML

   <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" />
    <link href="stylin.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
         $(function(){
            $(".button").toggle(function(){
                $("#blocks").animate({top: 200}, 500 );
            },
            function(){ 
                $("#blocks").animate({top: 760}, 500 );
            });
        });
    </script>
 <script type="text/javascript">
 $(document).ready( function() {
     $(document).scroll( function() {
     $("#blocks").animate({top: (780-$(this).scrollTop())}, 1000 );

    });
});
</script>
    </head>
    <body>
    <div id="main">
        <div id="photo">
        </div>
        <div id="blocks">
            <div id="button_block">
                <a href="#" class="button" ><img src="scrollup.png" /></a>
            </div>
        </div>
    </div>
    </body>
</html>

CSS

    #main {
    width: 1500px; 
    position:absolute;

}

#photo{
    position:fixed;
    top:0;
    width:1500px;
    height:780px;
    background-color:blue;
    z-index:0;

}

#blocks{
    position:absolute;
    top: 760px;
    width: 100%;
    height: 1000px;
    background-color: red;
    z-index:1;
}

#button_block{
    position:absolute;
    width:100%;
    height:64px;
    top:-32px;
}

.button
{
    display:block;
    position: absolute;
    right: 686px;
}

The blocks div should be sliding up while scrolling.

Focus on this section:

<script type="text/javascript">
     $(document).ready( function() {
         $(document).scroll( function() {
         $("#blocks").animate({top: (780-$(this).scrollTop())}, 1000 );

        });
    });
    </script>

Answer №1

 
#main {
    width: 1500px; 
    position:absolute;

}

#photo{
    position:fixed;
    top:0;
    width:1500px;
    height:780px;
    background-color:blue;
    z-index:0;

}

#blocks{
    position:absolute;
    top: 760px;
    width: 100%;
    height: 1000px;
    background-color: red;
    z-index:1;
}

#button_block{
    position:absolute;
    width:100%;
    height:64px;
    top:-32px;
}

.button
{
    display:block;
    position: absolute;
    right: 686px;
}

#scroll_to_top{
   display:none;
   position:fixed;
   bottom:15px;
   right:20px;
   opacity:0.8;
   z-index:9999;
}
#scroll_to_top:hover{
   opacity:1;
}
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" />
    <link href="stylin.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
         $(function(){
            $(".button").toggle(function(){
                $("#blocks").animate({top: 200}, 500 );
            },
            function(){ 
                $("#blocks").animate({top: 760}, 500 );
            });
        });
    </script>

    <script type="text/javascript">
        $(function(){
            var stt_is_shown = false;
            $(window).scroll(function(){
            var win_height = 300;
            var scroll_top = $(document).scrollTop(); 
            if (scroll_top > win_height && !stt_is_shown) {
                stt_is_shown = true;
                $("#scroll_to_top").fadeIn();
            } else if (scroll_top < win_height && stt_is_shown) {
                        stt_is_shown = false;
                        $("#scroll_to_top").fadeOut();
            }
            }); //Added a new if else statement
        });
    </script>
    
    <script type="text/javascript">
     $(document).ready( function() {
     $(document).scroll( function() {
     $("#blocks").animate({top: (780-$(this).scrollTop())}, 1000 );
}
    });
</script>
    </head>
    <body>
    <div id="main">
        <div id="photo">
        </div>
        <div id="blocks">
            <div id="button_block">
                <a href="#" class="button" ><img src="scrollup.png" /></a>
            </div>
        </div>
    </div>
    <a href="#" id="scroll_to_top"><img src="scrollup.png" /></a>
    </body>
</html>



   

I modified the existing if else statements and introduced a new one, give it a try!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the technique for adding a blur effect to the top and right side of a div element?

Here is a link to my fiddle: https://jsfiddle.net/aod4rmx8/ .background{ position: absolute; left: 0; width:50%; right: 0; bottom: 0; top:0; background-color: #000; /*box-shadow: 100px 0 40px 6px rgba(0, 0, 0, 1);*/ -webk ...

I have an Observable but I need to convert it into a String

Seeking assistance with Angular translation service and Kendo.UI components. In the Kendo.UI documentation, it mentions the use of MessageService for component translation implementation. To achieve this, an abstract class must be extended containing a m ...

Achieving repetitive progress bar filling determined by the variable's value

JSFiddle Here's a code snippet for an HTML progress bar that fills up when the "battle" button is clicked. I'm trying to assign a value to a variable so that the progress bar fills up and battles the monster multiple times based on that value. ...

Unveiling the power of Axios and Vue in fetching API data: The quest for

I've encountered a problem while trying to integrate my API with Vue/Axios. The issue arises when I attempt to store the data retrieved by Axios into an empty variable within the data object of my component. It throws an "undefined at eval" error. Can ...

Surprising Behavior of React's OnClick Event

Custom Component function ProductCard ({image, name, stats, id}){ let dispatch = useDispatch() let quantity = 1 return ( <> <div className="product-card" ...

Stuck on an endless loop of the Ionic splash screen

Currently, I am facing an issue with my Ionic 1 project. Everything was functioning properly until a few days ago when the splash screen stopped disappearing on iOS, although it still does on Android. I have searched for solutions on Google but haven' ...

What is the best way to progressively load sections of an HTML table?

I'm facing an issue with a table that is rebuilt asynchronously every time a new option is chosen from a dropdown. The HTML generation works fine, but sending it back across the wire breaks when the size is too large. One idea I had was to query sect ...

Why is it possible to import the Vue.js source directly, but not the module itself?

The subsequent HTML code <!DOCTYPE html> <html lang="en"> <body> Greeting shown below: <div id="time"> {{greetings}} </div> <script src='bundle.js'></script& ...

Adjust the height for just one md-tab

Looking to find a way to set the height of the content within an <md-tab> element to be 100% in a flexible manner. For example, consider the following structure: <body> <md-content> <md-tabs> <md-tab label= ...

Selenium Webdriver failing to select menuitem using text, xpath, or ID

Currently, I have some HTML code embedded in a SharePoint page that appears as follows: <span style="display:none"> <menu type='ServerMenu' id="zz28_RptControls" largeIconMode="true"> <ie:menuitem id="zz29_AddColumn" type="optio ...

What could be causing the format to be incorrect?

docker run -it -v "%cd%":/e2e -w /e2e cypress/included:6.2.1 --browser chrome When attempting to execute this command within Visual Studio Code, an error is encountered: docker: invalid reference format. See 'docker run --help' Vario ...

How to Override package.json Scripts in NPM

Is it possible to modify package.json scripts without changing the original file? I need to customize the memory allocation for a specific step, but altering the package.json will affect everyone. For example, our current script is: "script": { "dev": ...

Shorten Text - React Native

I currently have a React Native application with a FlatList component. The logic I initially implemented was to display an Expand/Collapse arrow whenever the content at the 100th position in the list is not empty. However, I now realize that this approach ...

Issues arise when Angular properties become undefined following the initialization or OnInit functions

There seems to be a peculiar issue with the properties of an angular component that I have set up. It appears that these properties are losing their values after the initialization process. The component is essentially a basic HTML file containing a video ...

Tips for ensuring that a server waits until a database connection is successfully established using node.js, MongoDB, and Express

I have a server.js file and a database.js file In the server.js file, I have the following code: const express = require('express'); var db = require('./database'); const app = express(); . . some get/post methods app.listen(3000); . ...

React Hamburger Menu not working as intended

I am facing an issue with my responsive hamburger menu. It is not functioning correctly when clicked on. The menu should display the navigation links and switch icons upon clicking, but it does neither. When I remove the line "{open && NavLink ...

Tips for aligning the first row of a table with a line of text

I need assistance aligning the first row of a table with a line of text. Specifically, I am trying to achieve ** some text ** |first row of table      |             ...

Utilize Vue to localize the click events of buttons on the webpage

I have a scenario where I have several buttons on a page, and when I click one to toggle its text, all of the buttons change. How can I utilize VUE to isolate functionality and make each button's @click event only affect the clicked button? In the cod ...

merging JavaScript objects with complex conditions

I am attempting to combine data from two http requests into a single object based on specific conditions. Take a look at the following objects: vehicles: [ { vId: 1, color: 'green', passengers: [ { name: 'Joe', ag ...

The multiple-choice selection tool is not displaying any choices

I am having an issue with my ng-repeat code in conjunction with the jquery multiple-select plugin. Despite using this plugin for a multiple select functionality, the options generated by ng-repeat are not visible. Below is the code snippet in question: & ...