incorporating a personalized API feed into a news ticker

Looking for help with setting up a ticker on my website to display headlines from my custom news API. I'm a bit lost on where to start, so any guidance would be appreciated.

Here's the API link - API Link

Is there a simple solution for this? Currently, I'm using a manual ticker and would prefer an automated one.

Here's the CSS code:

.slideText {
display: block;
width: 500px;
height: 30px;
background: red;
color: #fff;
overflow: hidden;
}
.slidingtext {
padding: 5px;
  animation-name: example;
  animation-duration: 15s;
  animation-iteration-count: infinite;
  transition: all 300ms ease;
}

@keyframes example {
  from {transform: matrix(1, 0, 0, 1, 500, 5)}
  to {transform: matrix(1, 0, 0, 1, -344, 5)}
}

and here's the HTML code:

<div class="slideText">
<div class="slidingtext">Lorem ipsum dolor sit amet.</div>
</div>

Any ideas on how to integrate the CSS and HTML for the ticker?

Answer №1

It seems like the solution you need is to utilize fetch in JavaScript to retrieve the information you are seeking.

Below is a basic code snippet that demonstrates how to fetch data from an API and display all the headlines in the console. You can modify this code to populate your ticker with the retrieved information and even schedule periodic updates.

const url =
  `https://script.googleusercontent.com/macros/echo?user_content_key=-WAEswRhz-A0GFJ7kVMHCtmEgGi3q5az_Vgw-M8X1TvM8Jer2UKhnDLhmiDCtGSKYwduFtp29EKPSP2NctqT6tB5DiSoHoStm5_BxDlH2jW0nuo2oDemN9CCS2h10ox_1xSncGQajx_ryfhECjZEnHo4zSljUNFhSoLskb1EQanQchoZ0NK0UbXpeUuc4BiuTzRrejx-uuzJ3Cy4pYPEhqLnfkWl2vSqJU1x8zviIhBUyyBe7Vdo1A&lib=MiQfzcgZEeK-gt9DdQ9yMvEauh1FhrIJO`;

fetch(url).then((response) => {
  return response.json(); // converting to JSON
}).then(info => {
  const data = info.data;
  for (let i = 0; i < data.length; i++) {
    console.log(data[i].headline);
  }
})

Answer №2

It seems like the timing of the animation could use some adjustment, but if you incorporate your code with this provided solution (credited to @A Haworth), you may get closer to your desired outcome.

Check out the code snippet below

$(function() {
  const url =
    `https://script.googleusercontent.com/macros/echo?user_content_key=-WAEswRhz-A0GFJ7kVMHCtmEgGi3q5az_Vgw-M8X1TvM8Jer2UKhnDLhmiDCtGSKYwduFtp29EKPSP2NctqT6tB5DiSoHoStm5_BxDlH2jW0nuo2oDemN9CCS2h10ox_1xSncGQajx_ryfhECjZEnHo4zSljUNFhSoLskb1EQanQchoZ0NK0UbXpeUuc4BiuTzRrejx-uuzJ3Cy4pYPEhqLnfkWl2vSqJU1x8zviIhBUyyBe7Vdo1A&lib=MiQfzcgZEeK-gt9DdQ9yMvEauh1FhrIJO`;

  fetch(url).then((response) => {
    return response.json(); // convert to json
  }).then(info => {
    const data = info.data;
    var idx = 0;

    const interval = setInterval(function() {
      $("#headline").html(data[idx].headline);
      
      console.log(data[idx].headline);  
      idx++;

      if (idx >= data.length) {
        idx = 0;
      }
    }, 8000);

    /*
    for (let i = 0; i < data.length; i++) {
      console.log(data[i].headline);  
    }
    */
  });
});
.slideText {
  display: block;
  width: 500px;
  height: 30px;
  background: red;
  color: #fff;
  overflow: hidden;
}

.slidingtext {
  padding: 5px;
  animation-name: example;
  animation-duration: 8s;
  animation-iteration-count: infinite;
  transition: all 300ms ease;
}

@keyframes example {
  from {
    transform: matrix(1, 0, 0, 1, 500, 5)
  }
  to {
    transform: matrix(1, 0, 0, 1, -344, 5)
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="slideText">
  <div id="headline" class="slidingtext">Lorem ipsum dolor sit amet.</div>
</div>

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

Is there a way for me to remove an uploaded image from the system

Here is an example of my HTML code: <input type='file' multiple/> <?php for($i=0;$i<5; $i++) { ?> <div class="img-container" id="box<?php echo $i ?>"> <button style="display: none;" type="submit" cl ...

A problem arises in Next.js when CSS is not rendering properly during Server Side Rendering

After creating my next.js application using the command npx create-next-app, I realized that the styles from the imported .css files are rendering correctly on Client Side Render but not on Server Side Render. The Next.js documentation states that importe ...

Challenges arise when creating a complementary php script for a natural language form

I'm a beginner at this and all the PHP I've tried hasn't been successful. I've come across examples of Natural Language forms, but none with functional PHP. While I am familiar with PHP in traditional forms, integrating the two has been ...

The stickiness of elements causes scrollbars to disappear in Chrome 77 on OSX

In this scenario, I have set up two tables where the first one has a sticky header achieved with position: sticky; top: 0px; https://codepen.io/seunje/pen/JjjRVLm The sticky header works perfectly fine, but there is an issue with scrollbars disappea ...

How can I show a tooltip when hovering over a tab using uib-tab?

Currently facing an issue with HTML/Bootstrap. My tabs are displayed using the uib-tabset directive on Angular Bootstrap (UI Bootstrap). I am trying to implement a tooltip that appears when hovering over a tab that is disabled. Does anyone know how I can a ...

What is the best way to create pages that showcase 10 posts each?

Currently, I am facing a challenge with displaying 100 posts using the Fetch API on one single page. I am looking for a way to create separate pages where each page would showcase only 10 posts. Below is my existing JavaScript code: fetch('htt ...

Ensure every individual input field in a Bootstrap form is validated using JavaScript before submitting the form

As a newcomer to Javascript, I am seeking assistance with validating each field in the form below without having to click on the submit button. Your help is greatly appreciated! <form action="" id = "form1" class = "form-group row"> & ...

if the input field is empty, eliminate the class

Within the code snippet, a function is present which performs the following actions. It searches for an email in the input field, then converts the email to an md5 hash, and finally generates a Gravatar URL that is inserted into a class using fadeIn(); ...

Dynamic Dropdown Validation During Form Submission

In the form, there are multiple rows displayed dynamically, each with a dropdown menu. The id for each dropdown is "ReasonCd#" where "#" increments from 0 based on the row number. By default, every dropdown has "Select Number" as the selected option, with ...

Activating the submit button only following confirmation that the image dimensions have been verified

I've written some code that checks whether selected pictures meet specific size and dimension constraints before enabling the submit button. However, there's an issue where the button might be enabled before verifying the last image due to delays ...

How can I achieve a floating effect for a div element that is contained within another element?

I have a container located within the body of my webpage. This container has margins on the left and right, as well as a specified width. Inside this container, there is a div element that extends beyond the boundaries of the container to cover the entire ...

There seems to be an issue with configuring placeholders in Tailwind CSS

After deciding to switch to using only tailwind CSS, I noticed that on a particular page there were inputs with transitions but no icon on the inner left side. Adjusting the colors and position of the placeholder helped prevent collisions between the icon ...

Struggling to Fetch Data from REST API with ng-repeat in Angular JS

After making a REST call from Angular JS and receiving a response from the API, I am looking to display messages on the front end using ng-repeat in my controller. $http({ url: "http://localhost:8080/services/AddConfig", method: "POS ...

Would you like to store modified CSS properties in a cookie?

Currently, I am facing a challenge while working on an ASPX page where CSS is used to define DIV properties. Although these properties can be altered on the page, they do not reflect when attempting to print them. Is there a method to modify CSS properties ...

Extract the text content from an HTML file while ignoring the tags to get the substring

Hello, I am a newcomer to the world of Web Development. I have an HTML document that serves as my resume, formatted in HTML. For example: html <p>Mobile: 12345678891 E-mail: <a href="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" ...

Developing web applications using a combination of PHP, MySQL, and

I am in need of creating an HTML form that functions as CRUD. This form should be able to record inputs such as "row_number", "channel_name", and "ip_address". It will store all the data in a MySQL table using the "ORDER BY row_number" function. The form i ...

How can the caption be aligned to the right of the image within a Bootstrap thumbnail in a precise manner, while also ensuring the button is positioned at the bottom of the caption?

Greetings! I am in the process of developing a website using bootstrap v3.3.2. My first query revolves around utilizing the grid system to correctly position the caption on the right side of the thumbnail, as illustrated below: <div class="container"& ...

Transform object into JSON format

Is there a way to transform an object into JSON and display it on an HTML page? let userInfo = { firstName: "O", lastName: "K", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b44476b445b05484446">[ema ...

What is the process for including a new column in a table created using the FixedTable jQuery plugin?

I'm a beginner with the jQuery FixedTable extension. How can I insert a new row into a table that already exists? I need guidance on how to accomplish this task. ...

VueJS component fails to remain anchored at the bottom of the page while scrolling

I am currently using a <md-progress-bar> component in my VueJS application, and I am trying to make it stay fixed at the bottom of the screen when I scroll. I have attempted to use the styles position: fixed;, absolute, and relative, but none of them ...