html - 使用媒体查询更改值数据属性

标签 html css media-queries

我创建了我的网站,但我遇到了一个 div 属性的问题。 我尝试在屏幕较小时更改该值。

我的 CSS 媒体查询:

@media screen and (min-width: 1000px) and (max-width: 2000px) {
#my-content { display: block; }
#my-slider {width: 670px;}

}

@media screen and (min-width: 0px) and (max-width: 1000px) {
#my-content { display: none; }   /* hide it elsewhere */
#my-text {font-size: 26px;}
#my-slider {content: attr(data-x : 700) ;}
}

我的 html:

<div id="my-slider" class="caption sfb" data-x="500" data-y="0" data-speed="900" data-start="900" data-easing="easeOutSine"><img src="code1.jpg" width="670px" height="500px"  alt="" /></div>

我想更改 data-x 但无法处理我发现的所有可能性...你能帮我吗?谢谢。

最佳答案

你最好的猜测是构建一些东西来检查你的浏览器的屏幕尺寸是否已经改变,而不是相应地设置 data 属性:

var resize = function() {
  var screenWidth = parseInt($('body').width()), mySlider = $( "#my-slider" );
  
  if (screenWidth < 1000) {
    mySlider.data('x','foo');
  }
  else if (screenWidth < 2000) {
    mySlider.data('x','bar');
  }
  else {
    mySlider.data('x','foo-bar');
  }
}

$( window ).resize(function() {
  resize();
});

$( document ).ready(function() {
  resize();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


<div id="my-slider" class="caption sfb" data-x="500" data-y="0" data-speed="900" data-start="900" data-easing="easeOutSine"><img src="code1.jpg" width="670px" height="500px"  alt="" /></div>

但是您必须记住,仅更改数据属性不一定会改变 slider 的行为。首先,您最好让自己拥有一个响应式 slider 。

关于html - 使用媒体查询更改值数据属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37609431/

相关文章:

html - Bootstrap 4 网格问题

html - 在 float div 中以百分比形式包裹高度的图像

html - 如何将 margin-top 应用于 <small> 标签?

html - @media 打印和(方向 :portrait) does not work if (orientation:landscape) is also specified

javascript - JavaScript 例程如何调用自身? (不是递归的!)

php - 在 php 中上传的文件没有返回函数

javascript - 提交按钮无需点击即可调用函数

php - 在网站上显示一个简单的条形图,没什么花哨的

iphone - iphone portrait、landscape 和 ipad portrait 的 3 个媒体查询

html - 在浏览器调整大小时下方 float 后将 div 居中