emoveClass('floating-player'); playerContainer.css({ position: originalStyle.position, top: originalStyle.top, left: originalStyle.left, width: originalStyle.width, height: originalStyle.height, margin: originalStyle.margin }); // 移除拖动事件 dragBtn.off('mousedown touchstart'); $(document).off('mousemove touchmove'); $(document).off('mouseup touchend'); closeBtn.off('click').remove(); dragBtn.remove(); isFloating = false; isDragging = false; } // 设置拖动功能 function setupDrag() { dragBtn.on('mousedown touchstart', function(e) { isDragging = true; // 获取初始位置 var containerOffset = playerContainer.offset(); offsetX = containerOffset.left; offsetY = containerOffset.top; // 获取鼠标/触摸初始位置 var clientX = e.type === 'touchstart' ? e.originalEvent.touches[0].clientX : e.clientX; var clientY = e.type === 'touchstart' ? e.originalEvent.touches[0].clientY : e.clientY; startX = clientX - offsetX; startY = clientY - offsetY; // 阻止默认行为和冒泡 e.preventDefault(); e.stopPropagation(); // 添加拖动时的类 playerContainer.addClass('dragging'); }); $(document).on('mousemove touchmove', function(e) { if (!isDragging) return; var clientX = e.type === 'touchmove' ? e.originalEvent.touches[0].clientX : e.clientX; var clientY = e.type === 'touchmove' ? e.originalEvent.touches[0].clientY : e.clientY; // 计算新位置 var newLeft = clientX - startX; var newTop = clientY - startY; // 限制在窗口范围内 var windowWidth = $(window).width(); var windowHeight = $(window).height(); var containerWidth = playerContainer.outerWidth(); var containerHeight = playerContainer.outerHeight(); newLeft = Math.max(0, Math.min(newLeft, windowWidth - containerWidth)); newTop = Math.max(0, Math.min(newTop, windowHeight - containerHeight)); // 应用新位置 playerContainer.css({ left: newLeft + 'px', top: newTop + 'px', right: 'auto', bottom: 'auto' }); e.preventDefault(); e.stopPropagation(); }); $(document).on('mouseup touchend', function() { if (isDragging) { isDragging = false; playerContainer.removeClass('dragging'); } }); } // 窗口大小变化时重新计算 $(window).resize(function() { if (!isFloating) { originalPosition = playerContainer.offset().top; } else { // 如果正在悬浮,确保播放器保持在可视范围内 var containerWidth = playerContainer.outerWidth(); var containerHeight = playerContainer.outerHeight(); var windowWidth = $(window).width(); var windowHeight = $(window).height(); var currentLeft = parseInt(playerContainer.css('left')); var currentTop = parseInt(playerContainer.css('top')); var newLeft = Math.max(0, Math.min(currentLeft, windowWidth - containerWidth)); var newTop = Math.max(0, Math.min(currentTop, windowHeight - containerHeight)); playerContainer.css({ left: newLeft + 'px', top: newTop + 'px' }); } }); }); -->