Issues stemming from undesirable behavior involving iframes and transitions

Hey everyone, I have a question regarding web development. I am quite new to this field and I'm facing an issue with getting videos to open with a fade-in and slide effect when a text link is clicked. I decided to use iframes instead of jQuery's slide function to ensure smooth animation. However, since I've never worked with iframes before, I'm encountering some unexpected behavior. When I click on a link, all the videos open up simultaneously. Has anyone experienced this before?

https://i.sstatic.net/XedTR.gif

Here is a snippet of my code (I've removed the text for clarity):

html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <link rel="stylesheet" type="text/css" href="styles.css">

</head>
<body> 
<div id="wrap">

<p id="text"> Lorem ipsum dolor...
    <span id="link1">link 1</span> 
    <span id="wrap1">
        <iframe  id="frame" class="rect" src="iframe.html" scrolling="no" marginwidth=0 marginheight=0></iframe>
    </span>
....Lorem ipsum dolor...
    <span id="link2">link 2</span>
    <span id="wrap2">
        <iframe  id="frame2" class="rect" src="iframe2.html" scrolling="no" marginwidth=0 marginheight=0></iframe>
    </span>
 ....Lorem ipsum dolor sit amet...
</p>

</div>
    <script type="text/javascript" src="jquery.js"></script>
    <script src="script.js" type="text/javascript"></script>
</body>
</html>

CSS:

#wrap{
    margin: 100px auto;
    width: 350px;
}

p{
    line-height: 2em;
}

iframe {
    border:0 none;
}

.rect{
    float: left;
    height: 0px;
    width: 350px;

    display: block;
    margin: 0px;
    padding: 0px;

    opacity: 0;

    transition-property: all;
    transition-duration: 2s;
    transition-timing-function: ease-in-out;
}

.open {
    height: 200px;
    width: 350px;
    opacity: 1;
}

#link1{
    color: red;
    cursor: pointer;    
}

#link2{
    color: red;
    cursor: pointer;
}

JavaScript:

  $(document).ready(function(){

    $("#link1").click(function(){


        if ($( ".rect" ).hasClass( "open" )){

            $(".rect").removeClass("open");

            $("body").find("iframe").contents().find("#pasc").get(0).pause();

        } else {

        $(".rect").addClass("open");

       $("body").find("iframe").contents().find("#vid1").get(0).play();
        }
    });

    $("#link2").click(function(){


        if ($( ".rect" ).hasClass( "open" )){

            $(".rect").removeClass("open");

            $("body").find("iframe").contents().find("#vid2").get(0).pause();


        } else {

        $(".rect").addClass("open");

         $("body").find("iframe").contents().find("#vid2").get(0).play();
        }  
    });
    });

iframe1:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="jquery.js"></script>   
    <script type="text/javascript" src="script.js"></script>
    <link rel="stylesheet" type="text/css" href="styles.css">       
</head>
<body>
    <video id="vid1" width="350" height="200" >
        <source src="video1.mp4" type="video/mp4">
    </video>
</body>
</html> 

iframe2:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="jquery.js"></script>   
    <script type="text/javascript" src="script.js"></script>
    <link rel="stylesheet" type="text/css" href="styles.css">       
</head>
<body>
    <video id="vid2" width="350" height="200" >
        <source src="video2.mp4" type="video/mp4">
    </video>
</body>
</html> 

Answer №1

The issue lies in the following line of code:

$(".rect").addClass("open");

By using this code, you are applying the class open to all elements that have the class .rect.

To target a specific iframe, you should utilize the iframe id like so:

$("#frame2").addClass("open");

Answer №2

Link to Functional Code.

Using $(".rect") will target all elements with the class rect. Similarly, calling $("body").find("iframe") will target all iframes on the page, which may be causing your issue here.

To avoid this, you can utilize the current object $(this) to reference the associated iframe with the clicked link by using:

$(this).next().find('.rect');

You can then store this in a variable and use it to execute other functions like so:

$("#link1").click(function(){
    var rect = $(this).next().find('.rect');

    if (rect.hasClass( "open" ))
    {
        rect.removeClass("open");
        rect.contents().find("#pasc").get(0).pause();
    } else {
        rect.addClass("open");
        rect.contents().find("#vid1").get(0).play();
    }
});

$("#link2").click(function(){
    var rect = $(this).next().find('.rect');

    if (rect.hasClass( "open" ))
    {
        rect.removeClass("open");
        rect.contents().find("#vid2").get(0).pause();
    } else {
        rect.addClass("open");
        rect.contents().find("#vid2").get(0).play();
    }
});

I hope this explanation proves helpful.

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

Centering text both vertically and horizontally over an image using CSS

I've been attempting to center the div banner_title both vertically and horizontally within another div in this way... .box { text-align: center; } .banner_title { font-size: 32px; position: absolute; top: 50%; width: 100%; } .banner_titl ...

MUI Autocomplete is failing to update the value when onChange event is triggered

I have successfully fetched autocomplete options from an API and it displays the pre-selected options from the API. However, I am encountering an issue where I am unable to delete or add a value (category) once selected. For an online demo, you can check ...

jQuery UI datepicker: Despite my attempt to convert with strtotime, the date is showing as 1969

I am currently working on a room reservation application that uses PHP and JQuery. I have implemented a datepicker widget for selecting dates, but I am facing an issue. When I try to convert the selected date from dd/mm/yyyy format to YYYY-mm-dd format in ...

Mastering the technique of showcasing landscape using CSS3

I'm working on an HTML and CSS3 report with multiple columns. I am trying to print it in landscape orientation using HTML and CSS3. I attempted rotating the body and table, but only half of the table is visible while the other half is cut off or hidde ...

What is the method to set the content-type of the requests generated by Ext.data.JsonStore to 'application/json'?

Being a newcomer to ExtJs and having a dislike for JavaScript, I find myself in a situation where I have to work with it. Below is the code that I currently have: var proxy = new Ext.data.HttpProxy( { url: 'sf/ ...

AngularJS - changes to service variables within ng-include view controller are failing to update parent controller

I am facing an issue with updating the values of an object stored in a service. The object's members are being set from a kendo-ui treeview's select event handler within a controller. This view, along with the treeview, is included in a second v ...

Encountered an error: Uncaught TypeError - Unable to access property 'active' as it is undefined within React code

<script type="text/babel"> class Frame extends React.Component{ render(){ return ( <div> <h2 className="os-animation" dat ...

Simultaneous beforeSave actions permitting repetitions

To prevent certain objects from being created, I incorporated a conditional in the beforeSave cloud function of that object type. However, when two objects are created at the same time, the conditional fails to work as expected. Check out my code snippet ...

Is it possible to create a pause between fade-ins and fade-outs in a background slideshow

I am facing an issue with my background slideshow where the images start to fadeout immediately after the fadein effect is complete. I would like to create a pause between each picture before the next transition begins. Below is my code: HTML: <script ...

What is the best method to reset the chosen option in a dynamic select dropdown using React?

I have a form set up with a Select dropdown that is populated dynamically from the server. The issue I'm facing is that after selecting an option from the dropdown and then saving or canceling the form, the selected value remains in the field when I ...

What is the best way to randomly choose a string from an array and store it in a variable?

For instance var names = array["bob","tom","jake"]; Is there a way to choose a random name from the array and then set it as the value of a variable? var randomName = I'm not sure what code belongs here ...

Unavailability of dates in Json ajax DatePicker

After retrieving database records of dates and converting them into a JSON Object, I am attempting to pass the JSON to javascript. The goal is to make the DatePicker UI dynamic by marking the dates in the JSON as unavailable on the calendar. Unfortunately ...

What are some ways I can safeguard my CSS from injected elements?

I have created an HTML widget that is inserted into various websites without the use of iframes. However, I am encountering issues where the CSS of some sites is affecting the appearance of my elements, such as text alignment, underlining, and spacing. Is ...

Can an image in CSS trigger the display of a <div> upon clicking it?

Here is an example of my HTML code: <html> <head> <style> img.settings { hight: 100px; width: 20px; } div.settings { position: fixed; left: 0px; top: 0px; right: 100%; bottom: 0px; background-color: #0000 ...

Tips for effectively formatting JSON data

Looking to optimize my JSON data formatting, specifically targeting two different keys within an object. Both keys are strings. I've created a fiddle showcasing the JSON structure: fiddle var json = [ { "Id": 1, "ReportId": 1, "ReportNa ...

Tips on clearing local storage when the browser is closed or a tab is closed in JavaScript, Angular, and React

Is there a way to automatically remove local storage or session storage data when closing the browser? Specifically, how can I delete the local storage details only when closing the final tab of a website with multiple tabs open? I've attempted variou ...

Javascript handles the sorting of elements within an array

I have spent time searching for a solution to my issue, but so far, I have not been successful in finding one. My PHP array is arranged exactly as I need it to be, but when I attempt to display it using JavaScript, the order gets distorted, specifically w ...

Optimizing White Space in Firefox

Recently completed the construction of my new website, it appears flawless when viewed on Chrome. However, upon inspecting it on Firefox (OS), I noticed that there is an approximately 15px space at the bottom of the page. I am struggling to identify what m ...

execute a separate function within an ajax request callback

I've created a function to validate usernames on my website. //Custom username validation function $('#username').blur(function() { var v=this.value; if(v == "") { addErrorMessage(this,'This field ...

Colorful legends in Chart.js graphs

I have successfully implemented a chart in my code. The background color for the legend is determined by $scope.colors, but in this example, I only have 3 static datasets. However, my issue arises when the data comes from the database. Is it possible for ...