Sketch a vertical line across the top of the picture

I developed a compact Soundcloud player for my website. Recently, I enhanced it with the feature that allows users to skip to different points in the track by clicking on the waveform. Now, I'm looking to add a visual indicator of the current position in the track, along with some text displaying the time at that point.

What would be the best way to overlay this information on top of the waveform image (which is currently displayed as an <img> tag)? And which specific elements should I utilize to achieve this?

Answer №1

If you want to achieve a scrolling effect on top of an image, you can use an absolutely positioned element, like a div, and set its position using the left property. For example, in this code snippet, the bar moves between 48px and 358px:

function play() {
  document.getElementById('pos').className = 'end';
}
#c {
  position: relative;
}
#pos {
  height: 50px;
  width: 1px;
  position: absolute;
  left: 48px;
  top: 1px;
  background: red;
  transition: left 5s linear;
}
#pos.end {
  left: 358px;
}
<div id="c">
  <div id="pos"></div>
  <img src="https://i.sstatic.net/LUNV8.png" />
</div>
<button onclick="play()">Play</button>


You can easily integrate this with your waveform listener by adding a simple function that updates the position based on the click event:

var pos = document.getElementById('pos');
var wave = document.getElementById('wave');

wave.addEventListener('click', function(e) {
  if (e.offsetX < 48)
    return;
  pos.style.left = e.offsetX + 'px';
});
#c {
  position: relative;
}
#pos {
  height: 50px;
  width: 1px;
  position: absolute;
  left: 48px;
  top: 1px;
  background: red;
}
<div id="c">
  <div id="pos"></div>
  <img id="wave" src="https://i.sstatic.net/LUNV8.png" />
</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

How to circumvent an email forwarder that removes attributes from an image tag and still track email opens?

While it may seem like an impossible task, I decided to give it a try. If the service I am utilizing removes the attributes of an image tag =>, is there a workaround available? I attempted inserting random unicode characters to disrupt the parser, but ...

Splitting a td tag into multiple columns dynamically with Angular

I am attempting to dynamically split the table element into separate columns. My desired layout should resemble this: https://i.sstatic.net/C81tg.png The values for name and surname are fixed at 1, but the values for subjects and grades can vary (there ma ...

Leveraging multiple ">" CSS selectors

Here is the HTML code to style: <table id="my-table"> <tr> <td> I would like to customize this </td> <td> <table> <tr> <td> Not looking to customize t ...

What is the best way to minify and concatenate multiple CSS files only after converting SCSS to CSS with Gulp?

When attempting to convert my scss files to css files using gulp, I encountered an issue. After writing the scss code, it is easily converted to css. However, I wanted to minify this css and save it in a different folder called 'min'. Within the ...

Exporting an HTML table to an Excel spreadsheet, allowing the user the flexibility to select a custom name for the file

When exporting an HTML table to Excel, everything works perfectly. However, I am looking for a way to allow the user to choose the file name for their download. This is how I currently export it: function exportToExcel() { window.open('data:applic ...

When the bootstrap alert message pops up, the content follows suit and flows seamlessly

I am currently facing a somewhat puzzling issue. I intend to showcase an alert message at the top of the screen upon the activation of an account. The problem arises when the message appears, causing the welcome blue box to shift downwards unexpectedly. Up ...

Can someone describe the cell phone symbol displayed in Google search results?

As I work on developing a mobile-friendly version of my website, I have noticed the presence of a mobile icon in Google search results. I am curious to learn more about what this mobile icon signifies and how I can effectively utilize it for optimizing m ...

Using JavaScript - Delay execution until all asynchronous ajax calls have completed before proceeding with an action

How can I create a function in JavaScript to execute code only after two asynchronous ajax calls have completed? The goal is to wait until both calls are done loading before executing additional code. Below is the code snippet I currently have: Link to JS ...

Set up a function to run for the first time using window.setInterval after a two-second delay, and then continue to

I have a JavaScript function that I want to run in a specific way: (function($){ $(document).ready(function(){ $(function () { index = 0; window.setInterval(function () { if (index > 10){ index ...

Issue with Navigation Bar when reducing screen width

I'm currently working on creating a navbar that includes an image logo, title, navigation links, and social links. To achieve this, I've divided the navbar into 4 separate divs. However, I've noticed that when I decrease the screen width usi ...

Attempting to gather details adjacent to a web form using HTML and CSS

I'm currently working on an HTML page that includes a form and some information. I've created two classes in CSS - one for the form called .contactform, and another for the information called .contactinformatie. My aim is to position the informa ...

Transforming a traditional HTML/CSS/JS website into a cutting-edge React application using Tailwind CSS styling

I have a unique website template complete with HTML, CSS, and all necessary assets. Now, I am looking to develop a React JS application using this template. Firstly, I am wondering: Since I typically use Tailwind CSS for my other projects, would it be mo ...

Having trouble getting the .each() method to function properly in jQuery

Looking for a solution to a similar issue here. Currently, I am attempting to iterate through multiple forms on a webpage and perform various actions with those forms. However, I am facing some challenges in making it work. Below is the code I am using: ...

Refresh a page automatically upon pressing the back button in Angular

I am currently working on an Angular 8 application with over 100 pages (components) that is specifically designed for the Chrome browser. However, I have encountered an issue where the CSS randomly gets distorted when I click the browser's back button ...

Ways to conceal text while still allowing it to be read by a screen reader

I am looking for a way to have a text updated by an action without displaying it on the webpage, but still have it announced by screen readers. I tried using display: none and visibility: hidden, however, these properties seem to not be accessible by scr ...

Dropdown element with PrimeNG adorned with a span

I am trying to create a simple form with text inputs and dropdowns. I have successfully implemented the textInput, but I am struggling with the dropdowns. Below is the code that I have so far: <div class="p-grid p-dir-col p-offset-2"> ...

Alert will only occur once the done function in jquery's .ajax method is executed

Something seems off and I'm struggling to understand the situation. I've used jQuery's .ajax() function to run a script that retrieves data from the database. The script is functioning properly and the data is being returned as expected: $. ...

The CSS property 'clear:both;' is not functioning properly on IE7 browsers whereas works fine on IE8/IE9, Firefox, Chrome, and other browsers

I have been receiving feedback from visitors about some issues on one of my websites. Those who reached out to us are using IE7 on Windows XP. I was able to recreate the problem by using IE7 or by mimicking it in compatibility mode with IE7 document mode o ...

Choosing a key from Angular's controller

Here is the combo box I am working with: <select data-ng-model="data.selectedChannel" data-ng-options="salesChannel.value for salesChannel in salesChannels"> </select> In my controller, I have a function that calls a factory to retrieve a k ...

What's the best way to ensure my text spans the entire width of the bootstrap navbar?

I want my text to span the entire width of the navigation bar, similar to this example Currently, it looks like this I am utilizing the most recent bootstrap CSS and also have my own custom stylesheet. Here is the HTML code for reference: <!doctype ...