Stop YouTube Video in SlickJS Carousel, Remove Placeholder Image Forever

Feel free to check out the CodePen example: http://codepen.io/frankDraws/pen/RWgBBw

Overview: This CodePen features an ad with a video carousel that utilizes SlickJS.

There are a total of 3 YouTube videos included in the carousel, with the SlickJS 'infinite' option enabled.

Challenges: 1. The task at hand is to ensure that the placeholder image disappears permanently upon clicking, even for the cloned elements. 2. Additionally, the YouTube video should pause when navigating between slides (including the clones). 3. It would be beneficial to streamline the final code for better efficiency.

Currently, the placeholder image disappears upon click, but reappears when scrolling through the carousel (depending on the clicked image and direction of scrolling).

My assumption is that somewhere in the SlickJS script, the image is being reinstated. Unfortunately, I am unsure of where in the SlickJS code this action occurs, and I am struggling to override it.

I have experimented with various approaches, such as:

$("#c1").on('click',function(){
  $(this).html('<iframe id="video01" width="100%" height="100%" src="http://www.youtube.com/embed/c07DH4HY2CA?list=&amp;wmode=opaque&amp;rel=0&amp;modestbranding=0&amp;showinfo=0&amp;autoplay=1&amp;controls=0&enablejsapi=1" frameborder="0" allowfullscreen="" style="width:305px; outline: 5px solid #fff; background:#000; margin:0 0 0 4px; padding:0"></iframe>');
  $("#c1 > img").hide();
});

$("#c2").on('click',function(){
  $(this).html('<iframe id="video01" width="100%" height="100%" src="http://www.youtube.com/embed/1bdtSFxBYW0?list=&amp;wmode=opaque&amp;rel=0&amp;modestbranding=0&amp;showinfo=0&amp;autoplay=1&amp;controls=0&enablejsapi=1" frameborder="0" allowfullscreen="" style="width:305px; outline: 5px solid #fff; background:#000; margin:0 0 0 4px; padding:0"></iframe>');
  $("#c2 > img").hide();
});

$("#c3").on('click',function(){
  $(this).html('<iframe id="video01" width="100%" height="100%" src="http://www.youtube.com/embed/Js_Jv5EzOv0?list=&amp;wmode=opaque&amp;rel=0&amp;modestbranding=0&amp;showinfo=0&amp;autoplay=1&amp;controls=0&enablejsapi=1" frameborder="0" allowfullscreen="" style="width:305px; outline: 5px solid #fff; background:#000; margin:0 0 0 4px; padding:0"></iframe>');
  $("#c3 > img").hide();
});

I have also attempted utilizing

$("#c3 > img").style.display = "none";
, among other methods, but unfortunately, none have resolved the issue.

Your assistance in tackling this challenge is greatly appreciated.

Answer №1

Check out this Code Pen for a demonstration using the YouTube IFrame API to control video playback: http://codepen.io/ccaspanello/pen/RWZqXb

var currentPlayer;

$('#c1').on('click',function(){
    var player = new YT.Player('c1', {
        width: '305',
        videoId: 'c07DH4HY2CA',
        events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
        }
    });
    currentPlayer = player;
});

// Additional Videos

function onPlayerReady(event) {
    event.target.playVideo();
}

function onPlayerStateChange(event){
    if (event.data == YT.PlayerState.PLAYING) {
        currentPlayer = event.target;
    }
}

It's possible to consolidate the onclick actions for video generation into functions. If you need assistance with this, feel free to reach out. Regarding the issue of preview videos reappearing, SlickJS clones content for looping purposes. After generating a YouTube video by clicking a preview, triggering an event to refresh the cloned objects may be necessary.

Answer №2

Interesting challenge to tackle. I haven't had the chance to experiment with SlickJS yet, but it seems like it doesn't have built-in video controls. In that case, I recommend exploring the YouTube IFrame API (https://developers.google.com/youtube/iframe_api_reference). By using the player.pauseVideo() function, you can easily pause the video when navigating through the next/previous buttons. This approach will also help tidy up the HTML replacements in your JavaScript code.

I'm nearly at a solution, but unfortunately, my battery is running low. I'll continue working on this and should have it completed by tomorrow morning.

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

The JSON response did not trigger the AJAX callback function

My AJAX function, written in coffeescript, is successfully returning values. However, neither the error nor the success callbacks are being triggered. $ -> $('#sf_field').autocomplete source: (request, response) -> $.ajax ...

Combining two objects by id and grouping identical key-value pairs together

var routePlan = [ { "id" : 1, "farmerName" : "Farmer1", "farmerId" : 1 }, { "id" : 2, "farmerName" : "Farmer2", "farmerId" : 2 }, { "id" : 1, "farmerName" : "Farm ...

Retrieve data from external URL using API

I am encountering a captchas page when attempting to retrieve JSON data from a URL through my API. Even though I am trying to access an array of JSON data containing openPrice, highPrice, price, and lowPrice, all I seem to get is a captcha form instead of ...

AngularJS and KendoUI integration experiencing delays during script evaluation

At the moment, I am conducting performance measurements and analysis on our AngularJS-KendoUI application in an effort to identify any bottlenecks. Following a helpful article and an informative presentation, I am using Chrome DevTools timeline tab to anal ...

What could be causing this axios.get request to fail?

I am currently enrolled in Colt Steele's Web Developer bootcamp and facing a challenge... In the tutorial, we are using axios as 'request' has been discontinued, so I am trying to follow along with this change. My goal is to set up a Get r ...

What is the best way to transfer $scope to a service in angular.js?

I have two services listed below. When navigating to a specific URL, I need to resolve "OtherService.promise". However, for certain routes, I would prefer to utilize a scope variable within the AppService method instead of the promise. How can I make thi ...

Unable to access the uploaded file on the server

Scenario : When a user uploads an image and clicks on the "save" button, I successfully save the image on the server with 777 permission granted to the folder... https://i.sstatic.net/gcIYk.png Problem : However, when I try to open the image, it does n ...

Developing custom functions in ejs for individual posts

In my blog, I have a feed where all users' posts are displayed dynamically using ejs. There is a comment section that remains hidden until the user clicks the comments button. Below is a snippet of my ejs file: <% posts.forEach(post => { %> ...

Having trouble accessing functions within the webpack bundle

As someone new to the world of JS library development, I have embarked on a journey to achieve the following goals: Creating a library with TypeScript Generating a bundle using webpack5 Publishing the library to npm Utilizing the library in other projects ...

Storing a Vue JS component as a string in a variable and displaying it using that variable

I am currently working on a project where I need to implement the following code snippet: <template v-for="(element, k) in place.data.content"> <data_elements[element.name].component></data_elements[element.name].component> </te ...

I am currently facing an issue with retrieving the class value that is being generated by PHP code

I am currently facing an issue with jQuery and PHP. Whenever I attempt to target the click event on the class ".id_annonce" in the dynamically generated PHP code, it doesn't retrieve the exact value of what I clicked. Instead, it always gives me a fi ...

Strategies for capturing and incorporating Meteor.Error notifications from Meteor.Methods into a client-side database?

I am currently working on creating an error notification panel in Meteor. I have set up a client-side MongoDB, but I am encountering an issue with pushing Meteor.Error messages into that client-side database using the throwError function. Currently, the er ...

Tips on saving HTML table information into a .txt document?

Welcome to my Unique HTML Table. <table> <thead> <tr> <th>First Name</th> <th>Last Name</th> <th>Email Id</th> <th>Phone Number</th> <th>Prefered C ...

Trouble fetching asynchronous data in Next.js Higher Order Component

Currently, I am in the process of creating a Higher Order Component (HOC) that will fetch user data from my API and then provide props to the wrapped component. This will allow the component to redirect the user based on their role. Although the HOC appea ...

How can I temporarily change an image when clicking on it using JavaScript?

I am working on an image section where I want to change the image for a few seconds when clicked and then revert back to the original image. Here is the code I have tried: <img src="contacticons.jpg" usemap="#image-map" id="imgName"> <a onclick ...

How can I dynamically update the content of the right side of the side navbar by clicking on navbar links?

I have a side-navigation bar with content on the right side. How can I display specific content on the right side when clicking on a navigation link? For example, only show the name when clicking on the name link and only show the password field when click ...

Idea: Develop a bookmarklet that generates a header form and deletes it upon submission

After much contemplation and experimentation, I have been grappling with the idea of creating a bookmarklet that displays a header when clicked on any webpage. This header will contain a small form that submits its contents to a server before disappearing. ...

Unable to loop through a list in JavaScript

<script type="text/javascript"> window.onload = function () { for (var i=0;i<@Model.listsinfo.Count;i++) { $('#work').append($('<div class="col-md-3" id="temp"><label for="tex ...

The Vue component fails to respond to updates in plugin data

I am currently working on implementing a feature where a component reacts to data changes in a custom Vue plugin. To achieve this, the plugin creates a new instance of a Vue object in its constructor: this._vm = new Vue({ data: { obj: null, stat ...

Commitment to provide the outcome of the second promise in case the first one encounters

I am trying to handle the scenario where I need to return the value of a second promise if the first one (value in cache) fails. Below is my code snippet, but I'm encountering an issue where 'resolve' is not defined. exports.getConfig = fu ...