Skip to content

Commit a14b5a7

Browse files
committed
Bug: Incorrect render height for the iframe of NetEase Music. #865
1 parent 3f08b7e commit a14b5a7

1 file changed

Lines changed: 38 additions & 29 deletions

File tree

‎source/js/src/utils.js‎

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -60,66 +60,75 @@ NexT.utils = NexT.$u = {
6060
embeddedVideoTransformer: function () {
6161
var $iframes = $('iframe');
6262

63-
/*
64-
* Supported Players. Extend this if you need more players.
65-
*/
63+
// Supported Players. Extend this if you need more players.
6664
var SUPPORTED_PLAYERS = [
6765
'www.youtube.com',
6866
'player.vimeo.com',
6967
'player.youku.com',
7068
'music.163.com',
7169
'www.tudou.com'
7270
];
73-
var pattern = new RegExp(SUPPORTED_PLAYERS.join('|'));
71+
var pattern = new RegExp( SUPPORTED_PLAYERS.join('|') );
7472

7573
$iframes.each(function () {
7674
var iframe = this;
7775
var $iframe = $(this);
76+
var oldDimension = getDimension($iframe);
77+
var newDimension;
7878

7979
if (this.src.search(pattern) > 0) {
80-
/*
81-
* Calculate the video ratio based on the iframe's w/h dimensions
82-
*/
83-
var videoRatio = ( $iframe.height() / $iframe.width() ) * 100;
8480

85-
// Add height for 163 music.
86-
if (this.src.search('music.163.com') > 0) {
87-
videoRatio += 10;
88-
}
81+
// Calculate the video ratio based on the iframe's w/h dimensions
82+
var videoRatio = getAspectRadio(oldDimension.width, oldDimension.height);
8983

90-
/*
91-
* Replace the iframe's dimensions and position
92-
* the iframe absolute, this is the trick to emulate
93-
* the video ratio
94-
*/
95-
$iframe
96-
.width('100%')
97-
.height('100%')
84+
// Replace the iframe's dimensions and position the iframe absolute
85+
// This is the trick to emulate the video ratio
86+
$iframe.width('100%').height('100%')
9887
.css({
9988
position: 'absolute',
10089
top: '0',
10190
left: '0'
10291
});
10392

104-
/*
105-
* Wrap the iframe in a new <div> which uses a
106-
* dynamically fetched padding-top property based
107-
* on the video's w/h dimensions
108-
*/
93+
94+
// Wrap the iframe in a new <div> which uses a dynamically fetched padding-top property
95+
// based on the video's w/h dimensions
10996
var wrap = document.createElement('div');
11097
wrap.className = 'fluid-vids';
111-
wrap.style.width = '100%';
11298
wrap.style.position = 'relative';
99+
wrap.style.marginBottom = '20px';
100+
wrap.style.width = '100%';
113101
wrap.style.paddingTop = videoRatio + '%';
114102

115-
/*
116-
* Add the iframe inside our newly created <div>
117-
*/
103+
// Add the iframe inside our newly created <div>
118104
var iframeParent = iframe.parentNode;
119105
iframeParent.insertBefore(wrap, iframe);
120106
wrap.appendChild(iframe);
107+
108+
// Additional adjustments for 163 Music
109+
if (this.src.search('music.163.com') > 0) {
110+
newDimension = getDimension($iframe);
111+
var shouldRecalculateAspect = newDimension.width > oldDimension.width ||
112+
newDimension.height < oldDimension.height;
113+
114+
// 163 Music Player has a fixed height, so we need to reset the aspect radio
115+
if (shouldRecalculateAspect) {
116+
wrap.style.paddingTop = getAspectRadio(newDimension.width, oldDimension.height) + '%';
117+
}
118+
}
121119
}
122120
});
121+
122+
function getDimension($element) {
123+
return {
124+
width: $element.width(),
125+
height: $element.height()
126+
};
127+
}
128+
129+
function getAspectRadio(width, height) {
130+
return height / width * 100;
131+
}
123132
},
124133

125134
/**

0 commit comments

Comments
 (0)