簡體   English   中英

根據按鈕中的點擊動態更改iframe大小

[英]Dynamically change iframe size depending on button click in it

我使用AngularJS在我的網站和客戶的網站中創建了一個聊天窗口(對話顯示在單擊氣泡中)。 當我想使用iframe實施它時,它的工作就像一個魅力,但是我必須定義一個手動的寬度和高度。

問題出在這里:當對話關閉時,氣泡始終可見,但是iframe的寬度和高度阻塞了屏幕的一部分(看圖片)...

在此處輸入圖片說明

所以我的答案就在這里:如何根據按鈕的點擊調整iframe的寬度和高度? 我已經嘗試將iframe包含在div中,但是它沒有用...

我的JS:

var a = document.createElement('iframe');
        a.setAttribute("id", "myIframe");
        a.setAttribute("src", "chats.html");
        a.setAttribute("scrolling", "no");
        a.style="width: 1px;min-width:100%;";
        var d = document.createElement('div');
        d.setAttribute("class", "chat");
        d.setAttribute("id", "chat");
        d.style="position: fixed;right: 0px;z-index: 9999;width: 400px;height: 755px;bottom: 0px !important;border: none;";
        document.querySelector('body').appendChild(d);
        document.querySelector('.chat').appendChild(a);
        document.querySelector('body').appendChild(s2);

如果不可能,我已經嘗試將對話容器和氣泡按鈕分離到2個iframe中,當我單擊按鈕時,它可以顯示帶有對話容器的第二個iframe。 但是問題是按鈕單擊在第二個div中沒有​​任何作用,即使它們位於相同的域中並且具有相同的ng-app和相同的ng-controller ...

請救我的命!

謝謝你們 !

看來您已經用香草Javascript創建了iFrame,這在Angular應用中可能不是一個好主意,但是我假設您的iFrame可以訪問Angular來回答我的問題。

  1. 為iFrame的兩種狀態創建一個樣式表類: chatOpenedchatClosed ,並在每個類中設置heightwidth

  2. 在您的iFrame中,使用<iframe ng-class="chatState"></iframe>

  3. 在控制器中創建$scope.chatState

  4. 每當您要擴展或收縮iframe時,請將$scope.chatState更改$scope.chatState 'chatOpened''chatClosed'

這是一個工作小提琴 ,其中單擊按鈕即可更改狀態。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM