The image seamlessly scrolls along with the page

Here is the code snippet for serving an image from a third-party system:

var bbimagebannerfile = "";

document.write("<a href=\"%%__REDIRECT%%\" target=\"_blank\"><img src=\"" + bbimagebannerfile + "\" border=\"0\" /></a>");

The image is currently located at the bottom of the page. I am looking to implement a functionality where the image scrolls along with the page once the user reaches its position, and then becomes static again when the user scrolls upwards. Any assistance on how to achieve this would be greatly appreciated.

Answer №1

Just for illustration:

<div id="fixed-image"></div> 

If we suppose that the HTML provided above is your served content. You can include this CSS:

#fixed-image {
    width: 100%;
    height: 100px;
    background-color: red;
    position: fixed;
    bottom: 0;
}

In your situation, you may want to consider adding a class with the specified styling.

JS Fiddle: http://jsfiddle.net/df6tmLrg/

Another demonstration using your JavaScript: http://codepen.io/FakeHeal/pen/qEgxKN (since jsfiddle does not permit document.write)

var bbimagebannerfile = "https://avatars2.githubusercontent.com/u/1038697?v=3&s=460";

document.write("<a href=\"%%__REDIRECT%%\" target=\"_blank\" class=\"bottom-fixed\"><img src=\"" + bbimagebannerfile + "\" border=\"0\" width="100" /></a>");

And the corresponding CSS:

.bottom-fixed {
    width: 100%;
    height: 100px;
    position: fixed;
    bottom: 0;
}

UPDATE: For those using jQuery, exploit .scroll():

var t =  $("#fixed-image").offset().top; 

$(document).scroll(function(){
    if($(this).scrollTop() > t)
    {   
        $("#fixed-image")
        .css('position', 'fixed') // modify position to fixed
        .css('top',0); // adjust top to zero
    } else {
        $("#fixed-image")
        .css('position', 'static') // modify position to static
        .css('top',0); // adjust top to zero
    }
});

Here's a demonstration: http://jsfiddle.net/df6tmLrg/2/

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

a guide on monitoring the status of a stripe transaction through a straightforward form and javascript

Currently, I am in the process of setting up Stripe checkout for my website. I have successfully implemented the payment form, but now I need to figure out how to redirect the user to another page after a successful payment. Below is the JavaScript code th ...

Can you explain the internal workings of string concatenation with the + operator in JavaScript?

What are the constraints when using the + operator for merging strings, such as special characters or HTML tags? There is a snippet of code (from a basic functioning website). I have the code for a specific page - www.example.com By solely modifying ...

Getting information from a transmit/sending in Ember

Is there a way to transfer data from Ember to an Express server? As per the official documentation, we can perform a save action in a router as shown below: export default Ember.Route.extend({ model(params) { var photo = this.get('store') ...

The error message "domElement" cannot be modified because it is read-only in the THREE.WebGLRenderer

I encountered an issue while attempting to initialize the WebGLRenderer: (Some unnecessary lines have been omitted) import * as THREE from "https://cdn.skypack.dev/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3c48544e5 ...

NodeJS Socket not transmitting file after connection with client

Having scoured the depths of various resources, including SO and Google, I have hit a roadblock. I am struggling to understand why the socket is failing to capture the uploaded file via the form; it simply stops after connecting. When I check the console, ...

Struggling to dynamically generate class names with clsx in combination with TailwindCss

Greetings! I am a JavaScript developer who is not very skilled yet, working with React and Next. Specifically, I am using this template When it comes to declaring component class names, I have been using a utility function that combines tailwind-merge and ...

Exploring the capabilities of Set() and the for..in loop in JavaScript

function removeDuplicates(menuArray) { let flatmenus = menuArray.flat();//This method combines child and parent arrays into one unique array let combinedMenu = new Set();//Creates an object that removes duplicate elements flatmenus.forEach(dish => ...

The most effective way to transmit data from an asynchronous call in Node.js while ensuring a reliable fallback routing structure

I have a request function that makes a call to an endpoint and retrieves data from it. When passing this data to an hbs template for rendering, the array is empty due to asynchronicity. Can someone guide me on the correct approach? Below is the code snippe ...

What is the best way to retrieve the value of scope.postresult in JavaScript within my HTML code?

I have the following code in my controller.js file where I am retrieving the value of "postresult". $scope.open= function(post) { $scope.postresult = post; } In the HTML snippet below, I am loading a DISQUS thread and need to access the "postr ...

Enable the use of CKEditor4 inline feature on various inline elements such as span and other

Is it possible to enable the CKEditor4 inline/contenteditable editing feature on <span> and other inline elements? I have searched the official documentation but cannot find any information on how to do this. Here is the markup I am using: <span ...

A guide to implementing infinite scrolling with vue-infinite-loading in Nuxt.js (Vue.js)

Currently, I am working on developing a web application using Nuxt.js (Vue.js). Initially, to set up the project, I used the command: vue init nuxt/express MyProject ~page/help.vue <template> <div> <p v-for="item in list"> ...

Is there a specific requirement for importing a React component into a particular file?

I am facing an issue with my component and two JavaScript files: index.js and App.js. When I import the component into index.js, it displays correctly on the screen. However, when I import it into App.js, nothing appears on the screen. Here is the code fr ...

What are some alternatives to tables for displaying two lines of headings and corresponding data?

Here is a snippet of HTML code I am working with: <div><span class='top'>Answered</span></div><div id="hour">15</div> <div><span class='top'>Not Answered</span></div ...

Testing React Hooks in TypeScript with Jest and @testing-library/react-hooks

I have a unique custom hook designed to manage the toggling of a product id using a boolean value and toggle function as returns. As I attempt to write a unit test for it following a non-typescripted example, I encountered type-mismatch errors that I' ...

Simple method for extracting data from JSON and saving it into an array with jQuery

I have a JSON file containing the resource "A" and literals "B", "C", and "D". My goal is to extract only the items B, C, and D, convert them into strings, and store them in an array. Below is the script I have written for this purpose: <script> // ...

Perform second level array grouping in JSON

I need help with grouping at the second level of a JSON string. The code I have works well for one level arrays in the JSON string. Array.prototype.groupBy = function(keyName) { var res = {}; this.forEach(function(x) { var k = x[keyName]; va ...

Changing the dynamic boundaries does not produce any results in the 'react-leaflet' library

Encountered an issue while trying to utilize the react-leaflet's bounds property. In my scenario, the application has predefined initial bounds in the state (referred to as fallback bounds) which are passed during the first component render. The archi ...

The method by which AngularJS identifies the appropriate property within a return value

In Angular, watchers are utilized to identify property changes and trigger a listener function. An example of a watcher declaration is shown below: $scope.food = "chicken"; scope.$watch( function() { return food; }, function(newValue, oldValue) { ...

Sending a URL via JQuery's POST method

Here is the Jquery function I am working with: function refreshTheGrid(myData, ceduleDateFrom, ceduleDateTo, paramOrderNo) { var myData2 = { CeduleDateFrom: ceduleDateFrom, CeduleDateTo: ceduleDateTo, ParamOrderNo: paramOrderN ...

Executing a JavaScript command to execute a PHP script and subsequently inserting HTML content into the appropriate row

Here is an easy-to-follow guide... Essentially, I have created an order form that initially consists of one row. <form id="orderform" name"orderForm" action="/secure/delivery-details.html" method="post"> <a id="add">+</a> <table id ...