Подтвердить что ты не робот

Можно ли не загружать iframe в скрытый div, пока не отобразится div?

В принципе, скрытый div, который отображается, когда клиент нажимает на ссылку. Этот div отображает форму php, которая задает клиентам вопросы о продукте, который он интересует.

Здесь код, который я нашел в Интернете, отлично подходит для меня с помощью jquery:

<script type="text/javascript">

$(document).ready(function(){


$('.show_hide').showHide({           
    speed: 500,  // speed you want the toggle to happen 
    easing: '',  // the animation effect you want. Remove this line if you dont want an effect and if you haven't included jQuery UI
    changeText: 1, // if you dont want the button text to change, set this to 0
    showText: 'REQUEST A QUOTE',// the button text to show when a div is closed
    hideText: 'CLOSE' // the button text to show when a div is open

}); 


});

</script>

css просто:

#slidingDiv, #slidingDiv_2{
height:300px;
background-color: #e2e2e2;
padding:20px;
margin-top:10px;
border-bottom:30px solid #000000;
display:none;
}

Здесь внешний java script:

(function ($) {
$.fn.showHide = function (options) {

    //default vars for the plugin
    var defaults = {
        speed: 1000,
        easing: '',
        changeText: 0,
        showText: 'Show',
        hideText: 'Hide'

    };
    var options = $.extend(defaults, options);

    $(this).click(function () { 

         $('.toggleDiv').slideUp(options.speed, options.easing);    
         // this var stores which button you've clicked
         var toggleClick = $(this);
         // this reads the rel attribute of the button to determine which div id to toggle
         var toggleDiv = $(this).attr('rel');
         // here we toggle show/hide the correct div at the right speed and using which easing effect
         $(toggleDiv).slideToggle(options.speed, options.easing, function() {
         // this only fires once the animation is completed
         if(options.changeText==1){
         $(toggleDiv).is(":visible") ? toggleClick.text(options.hideText) : toggleClick.text(options.showText);
         }
          });

      return false;

    });

};
})(jQuery);

Теперь я считаю, что в этом коде форма автоматически загружается, даже если она не показана. Я мог ошибаться, пожалуйста, поправьте меня, если да.

Вопрос, как я могу убедиться, что этот iframe внутри div будет загружаться только тогда, когда div больше не скрыт?

4b9b3361

Ответ 1

Вместо того, чтобы загружать iframe с помощью атрибута src, загрузите его вместо атрибута data-src. Для его значения укажите возможное местоположение, которое вы будете использовать, когда родительский div станет видимым.

<div class="hidden_element">
    <iframe data-src="http://msdn.microsoft.com"></iframe>
</div>

Когда вы показываете родительский div, выполните обратный вызов, который берет атрибут iframe data-src, и устанавливает его значение в атрибут src iframe, заставляя его загружаться.

// Show our element, then call our callback
$(".hidden_element").show(function(){
    // Find the iframes within our newly-visible element
    $(this).find("iframe").prop("src", function(){
        // Set their src attribute to the value of data-src
        return $(this).data("src");
    });
});

Демо: http://jsfiddle.net/bLUkk/