简体   繁体   English

使用javascript移动内容块以进行响应式设计

[英]Moving content blocks using javascript for responsive design

I'm working on a highly responsive website at the moment and I hit 2 areas where certain blocks of content need to move to others areas of the site. 目前,我正在一个高响应性的网站上工作,我碰到了两个区域,其中某些内容块需要移至该网站的其他区域。 It would not be possible to do so purely with CSS. 单纯使用CSS不可能做到这一点。 I suppose I could relatively reposition the blocks but as the dimensions change this isn;t really possible. 我想我可以相对地重新定位块,但是随着尺寸的改变,这是不可能的。

The option I am thinking of is, when a media query gets triggered, to then pull a block out of the page and append it in elsewhere where I need it. 我想到的选项是,在触发媒体查询时,将一个块从页面中拉出并附加到我需要的其他位置。

I realise this is not ideal bit what I am wanting to ask is if this is a reasonable thing to so. 我意识到这不是一个理想的选择,我想问的是这样做是否合理。

I know some of you may say reorder some of the markup but that is not possible. 我知道有些人可能会说要对一些标记重新排序,但这是不可能的。 As stated above, I know falling back to javascript is not ideal but it would suit this and I don't particularly wish to duplicate content just so I can avoid the use of javascript. 如上所述,我知道退回到javascript并不理想,但是它适合于此,我不希望重复内容只是为了避免使用javascript。

Flexbox would be perfect but support is not where I want it to be currently for me to use that. Flexbox会是完美的,但是支持并不是我现在希望使用它的地方。

What do people here think? 这里的人怎么想? Any other solutions? 还有其他解决方案吗?

The right way is to listen to media queries using MediaQueryList : 正确的方法是使用MediaQueryList监听媒体查询:

var mql = window.matchMedia("(max-width: 320px)");
mql.addListener(function(event) {
 if(event.matches) {
  // Window width is less than or equal to 320, do something cool.
 } else {
  // Window width is more than 320, do something else.
 }
});

The events will trigger when the query is either met or 'unmet'. 当查询满足或“未满足”时将触发事件。

Alternatively, you can listen to a resize event, but note your function will get triggered for every new dimension. 另外,您可以侦听resize事件,但请注意,您的函数将针对每个新维度触发。 (Assuming jQuery in the code below.) (假设下面的代码中是jQuery。)

$(window).resize(function() {
 if($(window).width() <= 320) {
  // Window width is less than or equal to 320, do something cool.
 } else {
  // Window width is more than 320, do something else.
 }
});

Like you said yourself though, using JS to make your layout responsive is generally NOT advisable. 就像您自己说的那样,通常不建议使用JS使布局响应。 You can never assume all your users have JS enabled and all goes well. 您永远不能假设所有用户都启用了JS并且一切顺利。

I would rather see you solve this by restructuring your HTML and CSS. 我希望您通过重组HTML和CSS来解决此问题。 If the content layout has to change a lot, try outputting a block of content in two different places in your HTML and toggling visibility with CSS media queries (setting one to display:none; and the other to display:block; ). 如果内容布局必须改变很多,请尝试在HTML中的两个不同位置输出一个内容块,并使用CSS媒体查询切换可见性(将一个设置为display:none;将另一个设置为display:block; )。 You should be able to solve most responsive layout issues by rethinking your website structure. 通过重新考虑您的网站结构,您应该能够解决最敏感的布局问题。

Others looking for a solution may be interested in the Bootstrap Toolkit JS library available here: https://github.com/maciej-gurban/responsive-bootstrap-toolkit 其他寻求解决方案的人可能会对Bootstrap Toolkit JS库感兴趣,该库可在此处获得: https : //github.com/maciej-gurban/sensitive-bootstrap-toolkit

Responsive Bootstrap Toolkit provides an easy way of breakpoint detection in JavaScript, detecting changes in currently active breakpoint, as well as executing any breakpoint-specific JavaScript code. 响应式Bootstrap工具包提供了一种简单的方法来检测JavaScript中的断点,检测当前活动断点中的变化以及执行任何特定于断点的JavaScript代码。

The SASS module enables quick and simple styling for elements needing different property values for each screen resolution. SASS模块可为每个屏幕分辨率需要不同属性值的元素提供快速简单的样式。

Then you can do things like: 然后,您可以执行以下操作:

(function($, document, window, viewport){
  // Listen to resize event
  $(window).bind('resize', function() {
    // Default 300ms poll delay
    viewport.changed(function() {
      // Debug
      console.log( 'Current breakpoint: '+ viewport.current() );
      // Trigger custom event
      $('body').trigger('viewportChanged', [viewport.current()]);
    }, 300)
  });

  // Register event listener
  $(document).on('viewportChanged', 'body', function(event, current) {
    console.log('Current breakpoint: '+ current);
  }

})(jQuery, document, window, ResponsiveBootstrapToolkit);

You could check out some of the already available responsive design HTML boilerplates like Twitter Bootstrap or Zurb Foundation . 您可以查看一些已经可用的响应设计HTML样板,例如Twitter BootstrapZurb Foundation Maybe their existing configurations satisfy your need. 也许他们现有的配置可以满足您的需求。

I have a similar problem on two websites and i do: 我在两个网站上也遇到类似的问题,我这样做:

JavaScript/jQuery with the window re size event and have breakpoints in JavaScript to. 具有窗口re size事件的JavaScript / jQuery,并且在JavaScript中具有断点。 I then remove the item and append/prepend it where i want it to be. 然后,我删除该项目并将其追加/添加到我想要的位置。

On my other website i use Twitter Bootstrap which is very responsive and looks nice. 在我的其他网站上,我使用Twitter Bootstrap,它反应灵敏且看起来不错。

I would personally go with Twitter Bootstrap as its a nice grid system. 我个人将Twitter Bootstrap视为一个不错的网格系统。 If your site is very complex and cant be done using Twitter Bootstrap them capturing the window re size event is the best way. 如果您的站点非常复杂且无法使用Twitter Bootstrap完成,则捕获窗口重新大小事件是最好的方法。

$(window).resize(function() {
  //Use $(window).width() and maybe some ifs/a switch to handle break points
  if($(window).width()<700){
    //Move it here
  }
 });

With CSS and JS it can be done :) You can clone the content to another section with jquery (append), then using media queries you can control what shows. 使用CSS和JS可以完成:)您可以使用jquery(追加)将内容克隆到另一部分,然后使用媒体查询来控制显示的内容。

Here is what I do: 这是我的工作:

I do the appendTo: 我执行appendTo:

$( $('.goto').html() ).appendTo('.mobile')

Here's an example I did: 这是我做的一个例子:

http://jsfiddle.net/Riskbreaker/vkfWd/ http://jsfiddle.net/Riskbreaker/vkfWd/

This might not be what you are looking for (since its really not moving it but cloning the content )but this is the way I do it. 这可能不是您要寻找的(因为它实际上并没有移动而是克隆了内容),但这就是我这样做的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM