Encountering a problem while attempting to loop through div content upwards in jQuery, specifically when displaying the latest news

Hey there, I'm fairly new to JQuery and I am working on dynamically appending content to a div. I need to continuously scroll the content of that div to the top. After some research, I found a solution that meets my requirements.

Here's a snippet of my HTML, take a look:

<div class="news_container" id="NewsContent">
    <div class="LatestNews">
        <div class="Content">
            with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        </div>
    </div>
</div>

Check out this example fiddler link here for reference

The code works perfectly in the fiddler above, but when implemented on an ASP.NET page, it throws an error stating:

"TypeError: jQuery.speed is not a function
optall = jQuery.speed( speed, easing, callback ),"

I'm puzzled by what could be wrong with the animate() function. Here's an excerpt of my code:

function CreateFunction() {
    $.fn.loopScroll = function () {
        var options = $.extend({
            direction: "upwards",
            speed: 50
        });

        var obj = $(this).find(".LatestNews");
        var text_height = obj.find(".Content").height();
        var start_y, end_y;

        if (options.direction == "upwards") {
            start_y = 0;
            end_y = -text_height;
        }

        var animate = function () {
            // setting up animation logic
            var distance = Math.abs(end_y - parseInt(obj.css("top")));
            
            obj.animate({top: end_y }, 1000 * distance / options.speed,
                function () {
                    obj.css("top", start_y);
                    animate();
                });
                
            animate();
        };

        $("#NewsContent").loopScroll({ speed: 120 });
    }
}

I'm having trouble understanding how optional parameters work. Can anyone suggest what might be causing the problem? Thanks in advance!

Answer №1

Your issue stems from not extending options in your plugin.

$.fn.loopScroll = function (p_options) { // You missed p_options
    var options = $.extend({
          direction: "upwards",
          speed: 50
      }, p_options); // You also missed extending p_options

You need to add the closing brace for $.fn.loopScroll =. Here is the corrected version:

function CreateFunction() {
    $.fn.loopScroll = function (p_options) {
        var options = $.extend({
            direction: "upwards",
            speed: 50
        }, p_options);

        var obj = $(this).find(".LatestNews");
        var text_height = obj.find(".Content").height();
        var start_y, end_y;
        if (options.direction == "upwards") {
            start_y = 0;
            end_y = -text_height;
        }
        var animate = function () {
            // Setup animation of "obj"
            // Calculate distance of animation    
            var distance = Math.abs(end_y - parseInt(obj.css("top")));
            // Duration will be distance / speed

            obj.animate({
                top: end_y
            }, 1000 * distance / options.speed,
            // Scroll upwards
            function () {
                // Scroll to start position
                obj.css("top", start_y);
                animate();
            });

        };
    } 
    $("#NewsContent").loopScroll({
       speed: 120
    });        
}

Another way to try it is this:

<script src="path/to/jquery.js"></script>
<script>
    ;(function ($) {
        $.fn.loopScroll = function (p_options) { // Getting new options
            var options = $.extend({
                direction: "upwards",
                speed: 50
            }, p_options); // Extending options
            var obj = $(this).find(".LatestNews");
            var text_height = obj.find(".Content").height();
            var start_y, end_y;
            if (options.direction == "upwards") {
                start_y = 0;
                end_y = -text_height;
            }

            var animate = function () {
                // Setup animation of "obj"
                // Calculate distance of animation    
                var distance = Math.abs(end_y - parseInt(obj.css("top")));
                // Duration will be distance / speed

                obj.animate({
                    top: end_y
                }, 1000 * distance / options.speed,
                // Scroll upwards
                function () {
                    // Scroll to start position
                    obj.css("top", start_y);
                    animate();
                });

            };

            obj.find(".Content").clone().appendTo(obj);
            $(this).on("mouseover", function () {
                obj.stop();
            }).on("mouseout", function () {
                animate(); // Resume animation
            });
            obj.css("top", start_y);

            animate(); // Start animation
        } // Make sure you include the closing braces in your code.
    }(jQuery));

    function CreateFunction() {
         $("#NewsContent").loopScroll({ speed: 120 });
    }
</script>

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

receiving ajax server updates

As a beginner in the world of ajax and wanting to receive constant updates from the server, I have been researching different methods to achieve this. However, I am unsure if my approach will work and would like some feedback on my understanding of the pro ...

Encountering the error message "Uncaught TypeError: Unable to assign value to property 'color' of an undefined object"

I'm facing an issue where I am trying to change the color of the button text to "white" when the button is clicked. However, upon clicking the button, I encounter an error that says "Uncaught TypeError: Cannot set property 'color' of undefin ...

Automatic cancellation of AJAX request

Having a strange issue that I need help with. I am using a standard ajax call to upload avatars from the user's PC to the server. However, sometimes I notice in Firebug that the request is being "Aborted" and marked in red after loading for some time ...

Revamp HTML <font size=1-7> with the use of CSS or JavaScript

I've been developing a prototype application that incorporates a plugin utilizing the deprecated HTML feature. Can I set custom pixel sizes for each font size ranging from 1 to 7? Currently, I'm contemplating using CSS zoom/scale properties, bu ...

Verify if the jQuery library has an existing style attribute

I've been working on a script to loop through my form inputs and check for the presence of the style attribute. Despite feeling like I'm close to getting it right, there seems to be an issue with my code. There are actually only 2 inputs with the ...

Tips for filling a Rails dropdown list using a JSON array

My Ant show page showcases detailed information about different types of ants. There are two drop downs on the page - one for environment: [indoor, outdoor], and another for diet: [sugar, fat, protein]. When a parameter is selected from each dropdown, it ...

Ways to verify multiple radio groups to ensure none have been left unchecked

https://i.sstatic.net/EoE1A.png Is there a more elegant solution to check if either "salad" or "side dish" is left unchecked after submission? I currently have a working approach, but it feels overly complex for such a simple task. This is my current me ...

What is the process for generating a flexible multi-column unordered list in a single column using Bootstrap 4?

I have been attempting to design a multi-column unordered list that can be centered in relation to its heading. Despite trying various methods found online to create two columns, I have struggled to properly center it under a heading or adjust the padding ...

Adding content to an empty element will not produce the desired result

I'm trying to show each character of a string, which is stored in an array, one at a time. However, when I use threadsleep(a) with the code found here: http://jsfiddle.net/thefiddler99/re3qpuoo/, all the characters are displayed at once. There seems t ...

Utilizing the button's id to display a partial view within Rails

I am working on a view that showcases a partial containing a list of events created by a user, each with an edit link alongside. My goal is to have a form partial appear below the event when the 'edit' link is clicked. To achieve this, I have su ...

Steps for Deploying ASP.NET Core 5 Web Site on IIS

After installing Visual Studio 2015 RC, I created a sample Web Site project and attempted to publish it using the publishing tool to the file system. Despite trying to target both the 'this folder' and 'wwwroot' options in IIS, I contin ...

How much space should be left from the edge for jQuery UI dialog to be

Typically, a dialog is centered using the following code: $(el).dialog('option', 'position', 'center'); Is there a method to specify a "minimum" distance from the side? For example, ensuring that the top position is always a ...

A menu displaying a selection of the text

When using IE9, my drop down list only displays part of the text. For example, if I select "Hello World", it only shows "Hello". I tried disabling the CSS and discovered that the issue is caused by the following code: .ui-widget { font-family: Verdana; f ...

Preventing an object from exceeding the boundaries of its designated space

My DIV is filled with user-generated content, which sometimes begins with an item that has a margin-top that overflows the container, creating a gap between it and preceding elements. I've discovered that changing the display property to either inline ...

Is there a way to stop the top nav from covering the first element of the side-nav in specific situations?

I'm grappling with the issue of how to prevent the side-nav from obscuring the top element in the vanilla Bootstrap 4 example provided below. Any tips? (Problem resolved - check below for solution) This problem is occurring not only on my website bu ...

What benefits come from dynamically loading and unloading JavaScript and CSS stylesheets?

Introduction: Currently, I am in the process of developing a website that will utilize ajax to change content without refreshing the main frame and images every time. The main frame has its own site.css stylesheet. Query 1: Considering the use of a sing ...

What is the best way to merge setInterval with mouseenter events?

I have successfully implemented code that refreshes a div using ajax. However, I am looking to add functionality so that the div only refreshes every 30 seconds when the tab is active. It seems that setInterval currently refreshes the div regardless of tab ...

What happens when jQuery interacts with AJAX and encounters multiple hash values in an application

I have been incorporating ajax features into some of the web apps I've developed, typically using a single hash (e.g. example.com/products#shirts). But now I'm considering loading the "products" page through ajax as well. While I understand that ...

retrieve the data-task-IDs from the rows within the table

I am currently working with a table that looks like this: <table id="tblTasks"> <thead> <tr> <th>Name</th> <th>Due</th> ...

Uncertainty arises from the information transmitted from the controller to the Ajax function

In one of my coffee scripts, I have an AJAX call that calls a method in a controller. The AJAX call is structured like this: auto = -> $.ajax url : '<method_name>' type : 'POST' data : <variable_name> ...